[英]Hoverinfo in an R plotly barchart
我正在使用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)
这使:
因此, hv_text
除了在悬停时出现之外,还会被绘制出来。
我的问题是如何获取未绘制的 hoverinfo 文本? 我尝试为hoverinfo
参数( text
, x
, y
)使用各种不同的值,但在所有情况下, hv_text
都会被绘制。
有很多方法可以做到这一点。 一种方法是使用hovertext
而不是text
。 对于条形图,如果您使用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)
另一种选择是使用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.