1
votes

I want have a stack barplot made in ggplot2 and Im applying the ggplotly() command to transform it into a interactive plotly graphic.

The issue now is that I want the plot to display on hover each value depending on the ITEM.

Here is what I mean...

enter image description here

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:

> 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.

If you get what I mean. Is there any way of doing this?

Dont want plotly to display the cummulative values.

1

1 Answers

2
votes

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)