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

Time:09-30

I'm having the following data 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))

I'm plotting a histogram for the column EK referenced by the different pBu values:

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

The histogram plot looks like this: enter image description here

Now I want to change the hovertemplate a little bit. I want to delete pBu and binwidth in the hovertemplate and make the title in hovertemplate bold. I thought this would work when replacing two commands (geom_histogram and ggplotly) by:

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

and

ggplotly(tooltip = "text")

But it doesn't work. How can I do this?

CodePudding user response:

You were almost there:

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

and ggplotly(tooltip = "text").

  • Related