[英]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.