如何控制“计数"?在ggplotly的工具提示中用R中的实心条形图

问题描述:

在此先感谢您提供的任何建议!我希望能够在工具提示中为面向公众的交互式图重新标记计数".

Thanks in advance for any advice you can offer! I'm hoping to be able to relabel "count" in the tooltip for a public facing interactive plot.

这是一个可复制的示例:

Here's a reproducible example:

library(plotly)
df <- data.frame(cat=c(rep("A", 5), rep("B", 7), rep("C", 10)),
                 time=c(rep("Time1", 3), rep("Time2", 13), rep("Time3", 6)))
ggplotly(ggplot(df, aes(x=time, fill=cat)) + geom_bar(position = "fill"))

我知道我可以使用 text = paste("Category:",cat,"Time:" time)控制工具提示中的时间和类别标签,但似乎无法弄清楚如何赋予伯爵更美的称号.

I know I can control the time and category labels in the tooltip with text=paste("Category:", cat, "Time:" time), but can't seem to figure out how to give the count a more aesthetic titling.

感谢您的时间!

也许有一个更简单的解决方案,但是您可以这样做:

Perhaps there's an easier solution, but you can do:

library(plotly)
df <- data.frame(cat=c(rep("A", 5), rep("B", 7), rep("C", 10)),
                 time=c(rep("Time1", 3), rep("Time2", 13), rep("Time3", 6)))
gg <- ggplotly(ggplot(df, aes(x=time, fill=cat)) + geom_bar(position = "fill"))
ggg <- plotly_build(gg)
for(i in 1:length(ggg$x$data)){
  text <- ggg$x$data[[i]]$text
  text <- gsub("count:", "Count:", text)
  text <- gsub("time:", "Time:", text)
  text <- gsub("cat:", "Cat:", text)
  ggg$x$data[[i]]$text <- text
}
ggg