You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ggplotly()绘制直方图时如何自定义修改hovertemplate?

问题原因

你的方案未生效主要是两个问题:

  1. 把binwidth参数误写进了aes()映射里,这个是直方图的全局配置参数,不需要做变量映射,放在aes内会导致多余字段被带入悬停信息。
  2. 直方图的频数、x轴区间值都是ggplot2做统计变换后生成的计算变量,不能直接调用原始数据的EK列,需要用after_stat()获取统计后的变量值。

修正后的完整代码

library(data.table)
library(dplyr)
library(ggplot2)
library(plotly)

# 原始数据表
dt.test <- structure(list(Year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L), GPNRPlan = c(100236L, 111328L, 
124213L, 127434L, 128509L, 130058L, 130224L, 130309L, 130342L, 
130392L, 130402L, 130843L, 132067L, 132597L, 133874L, 133943L, 
133950L, 135610L, 135886L, 137233L), TB.Info = c("Below TB", 
"In TB", "In TB", "In TB", "Below TB", "Over TB", "Below TB", 
"Below TB", "Over TB", "In TB", "In TB", "In TB", "In TB", "In TB", 
"Over TB", "In TB", "Over TB", "Over TB", "In TB", "In TB"), 
    EV = c(3847.65626027868, 5338.68193802084, 10300.9948652062, 
    5038.09710335868, 0, 0, 137484.47498131, 7831.27390184494, 
    164388.072187793, 407933.536699877, 150602.438125808, 100041.996984112, 
    5507.15565463884, 11504.3745586375, 248370.436164802, 64735.121047624, 
    10593.0020416499, 6417.30039359188, 17012.0465711677, 10495.4677317902
    ), EK = c(3811.92025173996, 5586.32697111958, 10585.2526883719, 
    4877.33454328664, 0, 0, 132990.852298368, 8191.60122513343, 
    164693.884494041, 407440.302209835, 154209.736782941, 90520.9528421677, 
    5606.91878115746, 11322.4688264219, 258826.81430578, 64668.573242372, 
    10465.1665234383, 6480.83212436458, 17031.1646456238, 10590.414631028
    ), pBu = c(0.1, 0.05, 0.1, 0.1, 0.05, 0.1, 0.1, 0.15, 0.1, 
    0.1, 0.1, 0.2, 0.15, 0.1, 0.05, 0.2, 0.2, 0.1, 0.1, 0.1)), class = c("data.table", 
"data.frame"), row.names = c(NA, -20L))

options(scipen = 999)
col <- c("#00943c", "#006551", "#2f52a0", "#fdc300")

EK2018 <- (dt.test %>% dplyr::mutate(pBu = as.factor(pBu)) %>%
           ggplot(aes(x = EK)) +
           # 修正后的直方图配置
           geom_histogram(aes(color = pBu,
                              text = after_stat(paste0(
                                "<b>EK分布统计</b><br>",
                                "<b>EK区间中心值: </b>", round(x, 2), "<br>",
                                "<b>计数: </b>", count
                              ))), 
                          binwidth = 20000, # 原0.3的binwidth不符合EK数值范围,可自行调整
                          fill = "transparent", 
                          alpha = 0.7, 
                          position = "dodge") + 
           ylab("Counts") +
           scale_color_manual(values = col) +
           theme(panel.background = element_blank(), axis.line = element_line(colour = "black"),
                 plot.title = element_text(lineheight = .8, hjust = 0.5, face = "bold"),
                 legend.title = element_blank(), 
                 legend.background = element_rect(fill = "white", size = 0.5, linetype = "solid", colour ="black"),
                 legend.box.background = element_rect(colour = "black"), legend.box.margin = margin(t = 1, l = 1))
           ) %>%
           # 仅读取自定义text作为悬停内容
           ggplotly(tooltip = "text")%>% 
           layout(annotations = list(x = 0.45 , y = 1.01, text = "<b>2018", showarrow = FALSE, xref = 'paper', yref = 'paper'))

# 查看最终效果
EK2018

效果说明

上述代码生成的交互式图表悬停信息仅包含自定义内容,不会出现多余的pBu、binwidth字段,悬停首行标题为粗体格式。如果需要调整悬停显示的内容,直接修改paste0内的文本即可。

内容的提问来源于stack exchange,提问作者Miko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 14:39:05