[英]Hoverinfo in an R plotly barchart
I'm plotting a horizontal bar chart using R
's plotly
package, and including hoverinfo
parameter:我正在使用
R
的plotly
package 绘制水平条形图,并包括hoverinfo
参数:
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.因此,
hv_text
除了在悬停时出现之外,还会被绘制出来。
My question is how to get the hoverinfo text not plotted?我的问题是如何获取未绘制的 hoverinfo 文本? I tried using various different values for the
hoverinfo
parameter ( text
, x
, y
) but in all cases the hv_text
gets plotted.我尝试为
hoverinfo
参数( text
, x
, y
)使用各种不同的值,但在所有情况下, hv_text
都会被绘制。
There are a lot of ways to do this.有很多方法可以做到这一点。 One way would be to use
hovertext
instead of text
.一种方法是使用
hovertext
而不是text
。 For bar charts, if you use text
without using hovertext
, it will be plotted.对于条形图,如果您使用
text
而不使用hovertext
,它将被绘制。
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
.另一种选择是使用
hovertext
和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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.