使用plotly编辑堆积条形图的hoverinfo [英] Edit hoverinfo of a stacked bar chart with plotly

查看:81
本文介绍了使用plotly编辑堆积条形图的hoverinfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取 plotlys hoverinfo 属性以仅显示堆积条形图的一个条形的单个部分时遇到了一些麻烦.更准确地说,这是情节主页上的一个小例子:

I am having some trouble getting plotlys hoverinfo attribute to display just a single part of one bar of a stacked bar chart. To be more precise, here is a small example from the plotly Homepage:

library(plotly)

y <- c('giraffes', 'orangutans', 'monkeys')
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(y, SF_Zoo, LA_Zoo)

plot_ly(data, x = ~SF_Zoo, y = ~y, type = 'bar', orientation = 'h', name = 'SF Zoo',
             marker = list(color = 'rgba(246, 78, 139, 0.6)',
                           line = list(color = 'rgba(246, 78, 139, 1.0)',
                                       width = 3))) %>%
  add_trace(x = ~LA_Zoo, name = 'LA Zoo',
            marker = list(color = 'rgba(58, 71, 80, 0.6)',
                          line = list(color = 'rgba(58, 71, 80, 1.0)',
                                      width = 3))) %>%
  layout(barmode = 'stack',
         xaxis = list(title = ""),
         yaxis = list(title =""))

如果我将鼠标移到一个栏上,则会显示两个标签:

If I move the mouse over one bar, then both labels are shown:

有没有办法在我将鼠标移到灰色条上时只显示灰色条的标签,将鼠标移到粉色条上时只显示粉色标签,等等?

Is there a way to just show the label of the grey bar when I move the mouse over the grey bar, and the pink label when moving the mouse over the pink bar, and so on?

推荐答案

您必须将悬停模式从悬停时比较数据"更改为悬停时显示最接近的数据".

You must change the hovermode from "Compare data on hover" to "Show closest data on hover".

要以编程方式实现此目的,您必须将 hovermode 属性设置为 closest (https://plot.ly/r/reference/#layout-hovermode).

To achieve this programatically you must set the hovermode attribute to closest (https://plot.ly/r/reference/#layout-hovermode).

对于 R,我认为您必须添加如下内容:layout(hovermode = 'closest')

For R I think you must add something like this: layout(hovermode = 'closest')

如果您想在生成的图中手动切换,您可以按照 sconfluentus 在您的问题下的评论进行操作:只需在双箭头模式栏按钮上单击左键.

If you want to switch this manually in the generated plot you can do it as sconfluentus commented under your question: Just click left on the double-arrow modebar button.

这篇关于使用plotly编辑堆积条形图的hoverinfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆