簡體   English   中英

R plotly 條形圖中的 Hoverinfo

[英]Hoverinfo in an R plotly barchart

我正在使用Rplotly 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參數( textxy )使用各種不同的值,但在所有情況下, 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)

在此處輸入圖像描述

另一種選擇是使用hovertexthovertemplate

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM