R plotly直方图悬停文本 [英] R plotly histogram hover text

查看:75
本文介绍了R plotly直方图悬停文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.只是一个简单的直方图.但我想要做的是自定义悬停文本,以便当我悬停时,它将显示该直方图栏中包含的所有物种.你能帮我吗?

This is my code. Just a simple historgram. But what I wanted to do is to customize the hover text so that when I hover, it will display all species included in that histogram bar. Can you help me?

iris %>% 
  plot_ly(x=~Sepal.Length, color=~Sepal.Width, text=~Species) %>% 
  add_histogram()

这是输出.但是当我悬停时,文本似乎只显示表格中的第一个物种.plotly_hist

Here's the output. But when I hover it seems the text is only displaying the first species in the table. plotly_hist

推荐答案

我不确定这是否可行.可能你对情节要求太多了.在尝试了一些选项后,我认为如果您希望不同的 Species 显示在工具提示中,有两种方法:

I'm not sure whether this is possible. Probably you are demanding too much from plotly. After trying some options I think there are two ways to go if you want the different Species to show up in the tooltip:

第一个选项是使用 hovermode = "unified" 使用堆叠直方图,如下所示:

First option is to use a stacked histogram using hovermode = "unified" like so:

library(plotly)

fig <- plot_ly()

fig <- fig %>% add_trace(data = filter(iris, Species == "setosa"), 
                         x = ~Sepal.Length,
                         color = ~Species,
                         text = ~Species,
                         type='histogram',
                         bingroup=1, showlegend = FALSE)

fig <- fig %>% add_trace(data = filter(iris, Species == "versicolor"),
                         x = ~Sepal.Length,
                         color = ~Species,
                         text = ~Species,
                         type='histogram',
                         bingroup=1, showlegend = FALSE)

fig <- fig %>% add_trace(data = filter(iris, Species == "virginica"),
                         x = ~Sepal.Length,
                         color = ~Species,
                         text = ~Species,
                         type='histogram',
                         bingroup=1, showlegend = FALSE)

fig <- fig %>% layout(
  hovermode="unified",
  barmode="stack",
  bargap=0.1)

fig

第二种选择是自己进行计算,即分箱和汇总并制作计数条形图.

The second option would be to make the computations yourself, i.e. binning and summarising and to make a bar chart of the counts.

iris %>% 
  mutate(Sepal.Length.Cut = cut(Sepal.Length, breaks = seq(4, 8, .5), right = FALSE)) %>% 
  group_by(Sepal.Length.Cut, Species) %>% 
  summarise(n = n(), Sepal.Width = sum(Sepal.Width)) %>% 
  tidyr::unite("text", Species, n, sep = ": ", remove = FALSE) %>%
  summarise(n = sum(n), Sepal.Width = sum(Sepal.Width) / n, text = paste(unique(text), collapse = "\n")) %>% 
  plot_ly(x = ~Sepal.Length.Cut, y = ~n, text = ~text) %>% 
  add_bars(marker = list(colorscale = "Rainbow"), hovertemplate = "%{y}<br>%{text}")

编辑 第三个选项是使用 ggplotly().通过这种方式,添加注释以显示每个 bin 的总数是一项简单的任务.这样我们就可以利用 ggplot2 中的 stats 层来完成所有的计算.据我所知,使用pure"无法那么容易地做到这一点.情节.

Edit A third option would be to use ggplotly(). This way it is an easy task to add annotations displayling the total numbers per bin. This way we can make use of the stats layers in ggplot2 which will do all the computations. To the best of my knowledge that couldn't be done that easily using "pure" plotly.

library(plotly)

ggplot(iris, aes(Sepal.Length, fill = Species)) +
  stat_bin(breaks = seq(4, 8, .5), closed = "left") +
  stat_bin(breaks = seq(4, 8, .5), closed = "left", geom = "text", mapping = aes(Sepal.Length, label = ..count..), inherit.aes = FALSE, vjust = -.5) +
  theme_light()

ggplotly()

这篇关于R plotly直方图悬停文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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