Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Stacked bar hover not working

Hi ,
Using the following data i cannot get the hover function to work (the stacked bar is created but the hover function does not display the text).
I have noticed that if i remove the “color=~variable” statement then it works but i don´t get the colors displayed by variable

“Sample ““variable”” ““value”””
““Sample01"” ““Staphylococcus”” 79.08”
““Sample01"” ““Malassezia”” 15.83”
"“Sample01"” ““Unknown”” 1.74

 plot_ly(mm, x = ~Sample, y = ~value, color = ~variable,
    type = "bar",hoverinfo="text",text=paste("Percent abundance:",mm$value,"<br>",paste(taxa,":",as.character(mm$variable),sep=""),sep="") ) %>%
    layout(margin=mar,xaxis=ang,bargap=0.9,legend=leg,title=paste(taxa," Abundance",sep=""),yaxis = list(title = 'Percent abundance'), barmode = 'stack')

The above code will generate this graph. (text hover not working but coloring by variable works.

Now if i remove the statement “color=~variable” and create an new graph below then hover is only working for one value and there is no coloring by variable.

Any idea to get both coloring by variable and hover text to work.

Thanks

Hey @danova,

Simplifying your code highlights the issue is with your text. Here are some examples of using text https://plot.ly/r/text-and-annotations/. The following is simplified which produces your desired hover outcome:

Sample <- c('Sample01', "Sample01", "Sample01")
variable <- c("staph", "Mala", "Unknown")
value <- c(79.08, 15.83, 1.74)

mm <- data.frame(Sample, variable, value)

plot_ly(mm, x = ~Sample, y = ~value, color = ~variable, type = "bar") %>%
  layout(bargap=0.9, barmode = 'stack')

Cheers,

Hi bcd,
Your example works but the text cannot be modified and this is important since my text is customized.

I have modified your example by adding the custom text and it does not display the text (as in the first graph). Seems to be something related to text ?? Any idea ??

Sample <- c(‘Sample01’, “Sample01”, “Sample01”)
variable <- c(“staph”, “Mala”, “Unknown”)
value <- c(79.08, 15.83, 1.74)

mm <- data.frame(Sample, variable, value)
text = paste(‘Percent abundance:’,mm$value,"
", paste(taxa,":",as.character(mm$variable),sep=""),sep="")

pl <- plot_ly(mm, x = ~Sample, y = ~value, color = ~variable, type = "bar",hoverinfo='text',text=~text) %>%
  layout(bargap=0.9, barmode = 'stack')

This should do the trick until bug is fixed.

l <- plotly_build(pl)$x
l$data <- lapply(l$data, function(tr) { tr[[“text”]] <- I(tr[[“text”]]); tr })
as_widget(l)

Suggest at the github site following url