简体   繁体   中英

Hoverinfo in an R plotly barchart

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:

在此处输入图像描述

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.

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)

在此处输入图像描述

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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