【问题标题】:Showing a list of corresponding labels for ggplotly hover显示 ggplotly 悬停的相应标签列表
【发布时间】:2021-09-22 06:56:03
【问题描述】:

我想在交互式箱线图中显示来自数据框的信息。对应的代码是:

p=ggplot(data = data,
       aes(x = GeoAreaName,  fill= cat, text= Indicator)) +
  geom_bar()

ggplotly(p)

“cat”是描述数据质量的分类变量,“GeoAreaName”是国家名称,“Indicator”是“cat”描述的数据集名称。

我得到的结果几乎就是我想要的:

但是,我希望仅当我将鼠标悬停在其上方时才显示指标的标签,即应该出现与类别“猫”相对应的名称列表,而不是每个单独的名称作为条形图中自己的段。

有什么建议吗?

编辑:数据摘录:

【问题讨论】:

  • 您能添加一个数据示例吗?似乎text 比您想要绘制的更精细。
  • @c0bra 添加了部分数据框
  • 对于阿塞拜疆,ggplot 需要为每个cat 绘制至少两个不同的indiacators。如果你事先聚合indicator 应该没问题。
  • 另外,最好将带有dput的示例数据放在SO上。
  • 一个更正:dataNew = data %>% group_by(GeoAreaName, cat) %>% summarize(Indicator = paste(Indicator, collapse=", "), count=n())ggplot(data = data, aes(x = GeoAreaName, y=count, fill= cat, text= Indicator)) + geom_bar(stat="identity") 绘图

标签: r ggplot2 plotly r-plotly ggplotly


【解决方案1】:

通过串联聚合指标。

dataNew <- data %>% 
    group_by(GeoAreaName, cat) %>%
    summarize(Indicator = paste(Indicator, collapse=", "), count=n())

绘图:

p <- ggplot(data = dataNew, aes(x = GeoAreaName, y=count, fill= cat, text= Indicator)) + 
    geom_bar(stat="identity")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2017-02-23
    相关资源
    最近更新 更多