简体   繁体   中英

How to display count of a subgroup with ggplotly() tooltip = "text" in R

does anyone know how I could display the count per group within the tooltip with ggplotly() using grom_bar , that shows already at least visually how many times Very Good cut and Colour E occured together in df? How should I address the count in the text aesthetics, in this case? And, I already know that if I change ggplotly(p, tooltip = "all") I see the count. I also checked this link How to control "count" in tooltip for ggplotly with filled bar plot in R . But I would like to have the flexibility of text so I could customize the tip. Thanks in advance!

library(ggplot2)
library(plotly)
data("diamonds")
p = ggplot(diamonds, aes(cut,   fill = as.factor(color), 
                   text = paste0("Cut: ", cut,"<br>Count: ", ???, "<br>Color: ",color, "<br>Count: ", ???))
      ) + 
geom_bar()

ggplotly(p, tooltip = "text")

I don't really understand what you really want to show, but maybe you can calculate the values before the plot.

p <- diamonds %>% 
  count(cut, color) %>% 
  add_count(cut, wt = n, name = "total") %>% 
  ggplot(aes(x = cut, y = n, fill = as.factor(color),
             text = paste0("Cut: ", cut, "<br>Count: ", total, "<br>Color: ", color, "<br>Count:  ", n))) +
  geom_col()
ggplotly(p, tooltip = "text")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM