繁体   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