简体   繁体   English

R plotly 条形图中的 Hoverinfo

[英]Hoverinfo in an R plotly barchart

I'm plotting a horizontal bar chart using R 's plotly package, and including hoverinfo parameter:我正在使用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)

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参数( textxy )使用各种不同的值,但在所有情况下, 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 .另一种选择是使用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