简体   繁体   English

在R的barplot中自定义悬停文本

[英]Plotly custom hover text in barplot from R

I want have a stack barplot made in ggplot2 and Im applying the ggplotly() command to transform it into a interactive plotly graphic. 我想在ggplot2和Im中使用堆栈条形图,然后应用ggplotly()命令将其转换为交互式的图形化图形。

The issue now is that I want the plot to display on hover each value depending on the ITEM. 现在的问题是,我希望图表根据项目在鼠标悬停时显示每个值。

Here is what I mean... 这就是我的意思

在此处输入图片说明

The plot is displaying a cumulative value for each of the items, so for example the green bar legend is displaying the red bar(because it comes before) PLUS the green bar value itself. 该图显示每个项目的累加值,例如,绿色条图例显示红色条(因为它先于之前)加上绿色条值本身。

I want each legend to display their own value based on the ITEM. 我希望每个图例根据项目显示其自身的价值。

Here is the DF: 这是DF:

> plotlydata
  Fecha Item  Suma
1 05/16  BAB 20540
2 05/16  BZO  1000
3 05/16  CLZ  1400
4 05/16  CMS 15000
5 07/16  BAB   400

So green bar instead of displaying 21540 (sum of previous 20540 and the own 1000) would display its own value of 1000. 因此,绿色的条形而不是显示21540(之前的20540和自己的1000的总和)将显示自己的值1000。

If you get what I mean. 如果你明白我的意思。 Is there any way of doing this? 有什么办法吗?

Dont want plotly to display the cummulative values. 不想以绘图方式显示累积值。

You can add that information by adding a text aesthetic and including it in the tooltip argument: 您可以通过添加文字美学并将其包含在工具提示参数中来添加该信息:

# they use the paste() trick on https://plot.ly/ggplot2/interactive-tooltip/
ggplot(d, aes(y = Suma, x = Fecha, fill = Item, text = paste("Suma:", Suma))) + 
  geom_bar(stat = "identity")

ggplotly(tooltip = c("text", "x", "fill"))

Sample data: 样本数据:

d <- read.table(text ="   Fecha Item  Suma
1 05/16  BAB 20540
2 05/16  BZO  1000
3 05/16  CLZ  1400
4 05/16  CMS 15000
5 07/16  BAB   400", header = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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