【问题标题】:ggplotly barchart and line chart with separate labels带有单独标签的ggplotly条形图和折线图
【发布时间】:2017-12-08 20:37:25
【问题描述】:

我有以下数据集:

       dput(head(table_age))
structure(list(Age = c("0-19", "20-24", "25-29", "30-34", "35-39", 
"40-44"), Freq_nominal = c(1321, 3446, 5354, 7233, 7471, 6623
), Freq_percent = c(1.92, 5.01, 7.79, 10.52, 10.86, 9.63), Percent = c("1.92%", 
"5.01%", "7.79%", "10.52%", "10.86%", "9.63%"), Nominal = c(1321, 
3446, 5354, 7233, 7471, 6623), female = c(655L, 1473L, 2275L, 
2929L, 3081L, 2830L), male = c(665L, 1918L, 3002L, 4258L, 4347L, 
3762L), female_percent = c(2.2, 4.94, 7.63, 9.82, 10.33, 9.49
), female_percent_label = c("2.2%", "4.94%", "7.63%", "9.82%", 
"10.33%", "9.49%"), male_percent = c(1.73, 4.99, 7.8, 11.07, 
11.3, 9.78), male_percent_label = c("1.73%", "4.99%", "7.8%", 
"11.07%", "11.3%", "9.78%")), .Names = c("Age", "Freq_nominal", 
"Freq_percent", "Percent", "Nominal", "female", "male", "female_percent", 
"female_percent_label", "male_percent", "male_percent_label"), row.names = c(NA, 
6L), class = "data.frame")

我使用 ggplotly 以交互方式将年龄绘制为条形图,并在工具提示中显示带有文本参数的自定义标签。

这是工作代码:

t <- ggplot(table_age, aes(as.factor(Age), Freq_percent, text1 = Nominal, 
                           text = paste0("Age group: ", Age), text2 = Percent))  +

  geom_bar(position="dodge",stat="identity", fill = "deepskyblue4", width = 0.5) +

  theme(legend.title=element_blank()) +
  xlab("") + ylab("") +
  theme_minimal()

ggplotly(t, tooltip = c("text1", "text2"))

我想在年龄条形图的顶部为每个性别覆盖一条线/点。在这方面,我有两个问题:

  1. geom_point 通过 plotly 绘制,但不是 geom_line(静态绘图时它工作正常)
  2. 我不知道如何将 geom_points 与 geom_bar 分开标记

这是我的尝试代码:

t <- ggplot(table_age, aes(as.factor(Age), Freq_percent, text1 = Nominal, 
                           text = paste0("Age group: ", Age), text2 = Percent))  +

  geom_bar(position="dodge",stat="identity", fill = "deepskyblue4", width = 0.5) +

  theme(legend.title=element_blank()) +
  xlab("") + ylab("") +
  theme_minimal()

t <- t + geom_point(data=table_age, aes(x=Age, y=female_percent), size = 2, color="magenta")
t <- t + geom_line(data=table_age, aes(x=Age, y=female_percent), colour="magenta")

ggplotly(t, tooltip = c("text1", "text2", "female"))

有什么想法吗?

【问题讨论】:

  • 您能否通过将dput(table_age) 的结果添加到问题中来使您的代码可重现?
  • 感谢您的评论。我刚刚更新了问题。

标签: r ggplot2 plotly


【解决方案1】:

geom_line() 需要(通常)group 美学,我们可以将其设置为常数以拥有一组 obs。这将显示geom_line

要显示不同的工具提示,我们可以使用单个“隐藏”text 美学(就像您已经做过的那样)并覆盖它。

在这种情况下,最好在每个 geom 中定义 aess,而不是在初始 ggplot() 调用中定义。

进入代码:

library(ggplot2)
library(plotly)

t <- ggplot(table_age)  +
    geom_bar(aes(as.factor(Age), 
                 Freq_percent, 
                 #text = paste0("Age group: ", Age), 
                 text1 = Nominal, 
                 text2 = Percent), 
             position = "dodge",
             stat = "identity",
             fill = "deepskyblue4",
             width = 0.5) +
    geom_point(aes(as.factor(Age), 
                   female_percent,
                   text1 = female), 
               size = 2, 
               color="magenta") +
    geom_line(aes(as.factor(Age), 
                  female_percent,
                  group = 1), 
              color = "magenta") +
    theme(legend.title = element_blank()) +
    xlab("") + 
    ylab("") +
    theme_minimal()
#> Warning: Ignoring unknown aesthetics: text1, text2
#> Warning: Ignoring unknown aesthetics: text1

t

ggplotly(t, tooltip = c("text1", "text2"))

【讨论】:

  • 感谢您的解决方案。此外,您的代码结构整齐。我也应该为此付出一些努力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
相关资源
最近更新 更多