如何使用 ggplotly() 工具提示 = R 中的子组"text"显示子组的计数



有谁知道我如何使用grom_bar在工具提示中显示每个组的计数,ggplotly(),至少已经直观地显示了Very GoodcutColourE在 df 中一起发生的次数?在这种情况下,我应该如何解决text美学中的计数?而且,我已经知道,如果我改变ggplotly(p, tooltip = "all")我会看到计数。我还检查了这个链接 如何在工具提示中控制"计数" 在 R 中使用填充条形图的 ggplotly . 但我希望有text的灵活性,这样我就可以自定义提示。 提前感谢!

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

我真的不明白你真正想展示什么,但也许你可以在绘图之前计算值。

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

最新更新