如何控制“计数"?在ggplotly的工具提示中用R中的实心条形图 [英] How to control "count" in tooltip for ggplotly with filled bar plot in R

查看:51
本文介绍了如何控制“计数"?在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

这篇关于如何控制“计数"?在ggplotly的工具提示中用R中的实心条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆