Home > other >  Hoverinfo in an R plotly barchart
Hoverinfo in an R plotly barchart

Time:08-12

I'm plotting a horizontal bar chart using R's plotly package, and including hoverinfo parameter:

df <- data.frame(n = 17:10, name = paste0("n",1:8), hv_text = paste0("t",1:8), stringsAsFactors = F)
df$name <- factor(df$name, levels = rev(df$name))
library(plotly)
plot_ly(marker = list(line = list(width = 1, color = "gray")), x = df$n, y = df$name, type = 'bar', text = df$hv_text, hoverinfo = "xxx", showlegend = F)

Which gives:

enter image description here

So the hv_text gets plotted in addition to appearing when hovered over.

My question is how to get the hoverinfo text not plotted? I tried using various different values for the hoverinfo parameter (text,x,y) but in all cases the hv_text gets plotted.

CodePudding user response:

There are a lot of ways to do this. One way would be to use hovertext instead of text. For bar charts, if you use text without using hovertext, it will be plotted.

plot_ly(df, marker = list(line = list(width = 1, color = "gray")), 
        x = ~n, y = ~name, type = 'bar', 
        hovertext = ~hv_text, hoverinfo = "text", showlegend = F)

enter image description here

Another option would be to use hovertext and hovertemplate.

plot_ly(df, marker = list(line = list(width = 1, color = "gray")), 
        x = ~n, y = ~name, type = 'bar', hovertext = ~hv_text, 
        hovertemplate = "Name: %{y}<br>Text: %{hovertext}<extra></extra>", 
        showlegend = F)

enter image description here

  • Related