1
votes

I am having some trouble getting plotlys hoverinfo attribute to display just a single part of one bar of a stacked bar chart. To be more precise, here is a small example from the plotly Homepage:

library(plotly)

y <- c('giraffes', 'orangutans', 'monkeys')
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(y, SF_Zoo, LA_Zoo)

plot_ly(data, x = ~SF_Zoo, y = ~y, type = 'bar', orientation = 'h', name = 'SF Zoo',
             marker = list(color = 'rgba(246, 78, 139, 0.6)',
                           line = list(color = 'rgba(246, 78, 139, 1.0)',
                                       width = 3))) %>%
  add_trace(x = ~LA_Zoo, name = 'LA Zoo',
            marker = list(color = 'rgba(58, 71, 80, 0.6)',
                          line = list(color = 'rgba(58, 71, 80, 1.0)',
                                      width = 3))) %>%
  layout(barmode = 'stack',
         xaxis = list(title = ""),
         yaxis = list(title =""))

If I move the mouse over one bar, then both labels are shown:

enter image description here

Is there a way to just show the label of the grey bar when I move the mouse over the grey bar, and the pink label when moving the mouse over the pink bar, and so on?

1
If I remember clearly on the graphs I have made in Plotly you just click on that third big-wide arrow from the plotly logo, it makes it a single hover. The double arrow to its right makes it show both data sets in the bar.sconfluentus

1 Answers

1
votes

You must change the hovermode from "Compare data on hover" to "Show closest data on hover".

To achieve this programatically you must set the hovermode attribute to closest (https://plot.ly/r/reference/#layout-hovermode).

For R I think you must add something like this: layout(hovermode = 'closest')

If you want to switch this manually in the generated plot you can do it as sconfluentus commented under your question: Just click left on the double-arrow modebar button.