使用ggplotly()时如何修改直方图的hovertemplate?

How to modify the hovertemplate of histogram when using ggplotly()?

我有以下数据table dt.test:

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))

我正在为不同 pBu 值引用的列 EK 绘制直方图:

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, binwidth = 0.3), 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))
           
           ) %>%
           ggplotly()%>% 
           layout(annotations = list(x = 0.45 , y = 1.01, text = "<b>2018", showarrow = FALSE, xref = 'paper', yref = 'paper'))

直方图如下所示:

现在我想稍微改变一下 hovertemplate。我想删除 hovertemplate 中的 pBubinwidth 并将 hovertemplate 中的标题设为粗体。 我认为这在将两个命令(geom_histogramggplotly)替换为:

时会起作用
geom_histogram(aes(color = pBu, binwidth = 0.3, 
text = paste("<b>Counts: </b>", count, "<br>", "<b>EK: </b>", EK)), fill = "transparent", 
alpha = 0.7, position = "dodge") +

ggplotly(tooltip = "text")

但它不起作用。我该怎么做?

你快到了:

 geom_histogram(
   aes(color = pBu, binwidth = 0.3, 
       text = paste("<b>Counts: </b>", ..count.., "<br>", "<b>EK: </b>", dt.test$EK)), fill = "transparent", 
   alpha = 0.7, position = "dodge") 

ggplotly(tooltip = "text").