【问题标题】:R plotly histogram hover textR plotly直方图悬停文本
【发布时间】:2020-06-24 15:11:54
【问题描述】:

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

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

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

【问题讨论】:

    标签: r plotly


    【解决方案1】:

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

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

    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
    

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

    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 中的统计层来完成所有的计算。据我所知,使用“纯”情节无法轻易做到。

    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()
    

    【讨论】:

    • 谢谢!这正是我要找的!
    • 跟进。如果我做第一个答案,如何自定义每个条形图顶部的数据标签,使其值类似于直方图(条形图中所有项目的计数)?
    • 嗨@jassybels。又看了一眼。不幸的是,除了切换到ggplotly 之外,我没有找到解决此问题的方法。通过这种方式,您可以利用 ggplot2 的所有魔力,并且仍然拥有交互式绘图图表。看看我的编辑。
    • 嗨 Stefan,如果你有时间,可以看看我的问题吗? stackoverflow.com/questions/66161597/…谢谢
    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2021-10-03
    • 2015-04-10
    • 2018-10-06
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多