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

Posted

技术标签:

【中文标题】使用ggplotly()时如何修改直方图的hovertemplate?【英文标题】:How to modify the hovertemplate of histogram when using ggplotly()? 【发布时间】:2021-11-22 03:03:00 【问题描述】:

我有以下数据表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。我想删除悬停模板中的pBubinwidth 并使悬停模板中的标题加粗。 我认为通过以下方式替换两个命令(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")

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

【问题讨论】:

【参考方案1】:

你快到了:

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

【讨论】:

感谢您的帮助:) 哦不!午休后,我收到一条错误消息(但它在午饭前工作)。 stat_bin() using bins = 30 Pick better value with binwidth. ERROR: Elements must equal the number of rows or 1。发生了什么事?不知道为什么?

以上是关于使用ggplotly()时如何修改直方图的hovertemplate?的主要内容,如果未能解决你的问题,请参考以下文章

在 ggplot/ggplotly 中使用 facet_wrap 时如何防止 y 轴挤压标签?

当我在 plotly 中使用 ggplotly 函数时,为啥文本注释会丢失?

在 R 函数 ggplotly 中(来自 plotly 包),如何调整标签内容

与 ggplotly() 结合使用时,ggplot2 堆叠条似乎被分成许多不同的切片

使用ggplotly时geom_tile瓷砖移位

使用 ggplotly() 显示绘图时,绘图字幕丢失