【问题标题】:Legend ggplot figure doesn't show for means, errorbars and coloured rectangle图例 ggplot 图不显示均值、误差线和彩色矩形
【发布时间】:2018-04-04 06:23:52
【问题描述】:

我一直在 R 中制作这个均值散点图,我知道我应该在图形图中包含所有感兴趣的变量作为美学。但是,在我创建的图中(见下文),图例没有显示。

我感兴趣的变量是均值、误差线和彩色矩形。任何人都知道如何以智能或手动方式绘制它们?

df <- data.frame(weeks = c(-1, 0, 1, 2, 3, 4),
             mean = c(64, 65, 66, 66, 66, 67),
             lowerCI = c(63.4, 64.9, 64.5, 63.8, 62.1, 66.8),
             upperCI = c(65.6, 65.1, 66.5, 67.2, 68.9, 67.2))

sp_ts <- ggplot(data = df,
            aes(x = weeks,
                y = mean))

sp_ts +
  geom_point(shape = 15,
         size = 4) +
  geom_errorbar(aes(ymin = lowerCI,
                ymax = upperCI),
            width = 0.05,
            size = 0.5) +
  labs(title = "Scatterplot of means",
   x = "Weeks",
   y = "Means") +
  scale_x_continuous(limits = c(-1, 4),
                 breaks = c(-1, 0, 1, 2, 3, 4)) +
  scale_y_continuous(limits = c(62, 69),
                 breaks = c(61, 62, 63, 64, 65, 66, 67, 68, 69)) +
  annotate("rect", xmin = -Inf, xmax = 2, ymin = -Inf, ymax = Inf, fill = "light blue", alpha = 0.2) +
  annotate("rect", xmin = 2, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "blue", alpha = 0.2) +
  theme_bw() +
  theme(panel.grid.minor = element_blank())

【问题讨论】:

  • 您没有图例,因为您没有在上面指定任何要放入图例的内容。这个传说应该区分什么?
  • 我想让图例告诉读者,正方形是平均值,误差线是 95% 置信区间,彩色矩形是不同的组。我想我也可以在图表下方的评论中执行此操作,但我认为最好将其包含为图例
  • 这不是传奇的典型目的。通常,该信息属于图表上方或下方的标题。图例专门将图形的特征​​(线型、颜色、填充、点类型)与数据子集(公司 A 与公司 B;处理 A 与处理 B)等进行映射。
  • 我知道这些图表在科学文献中的使用方式,但出于交流目的,我更喜欢在图表中包含尽可能多的信息。我的读者通常不喜欢大段文字

标签: r ggplot2 legend


【解决方案1】:

这是显示图例的一种方式(取自answer

library(ggplot2)

sp_ts1 <- sp_ts +
  geom_point(shape = 15, size = 4) +
  geom_errorbar(aes(ymin = lowerCI, ymax = upperCI),
            width = 0.05,
            size = 0.5) +
  labs(title = "Scatterplot of means",
   x = "Weeks",
   y = "Means") +
  scale_x_continuous(limits = c(-1, 4),
                 breaks = c(-1, 0, 1, 2, 3, 4)) +
  scale_y_continuous(limits = c(62, 69),
                 breaks = c(61, 62, 63, 64, 65, 66, 67, 68, 69)) +
  annotate("rect", xmin = -Inf, xmax = 2, ymin = -Inf, ymax = Inf, 
           fill = "light blue", alpha = 0.2) +
  annotate("rect", xmin = 2, xmax = Inf, ymin = -Inf, ymax = Inf, 
           fill = "blue", alpha = 0.2) +
  theme_bw() +
  theme(panel.grid.minor = element_blank())

sp_ts2 <- sp_ts1 +
  geom_point(aes(color = "Mean"), shape = 15, size = 4) +
    geom_errorbar(aes(ymin = lowerCI, ymax = upperCI,
                      color = "95% CI"),
            width = 0.05,
            size = 0.5) +
  scale_color_manual(name = "Legend", values = c("#666666", "#1B9E77")) +
  guides(colour = guide_legend(override.aes = list(linetype = c("solid", "blank"), 
                                                   shape = c(NA, 15))))
sp_ts2

要将 95% CI 显示为垂直线,请使用 geom_linerange

sp_ts3 <- sp_ts1 +
  geom_point(aes(color = "Mean"), shape = 15, size = 4) +
  geom_linerange(aes(ymin = lowerCI, ymax = upperCI, 
                       color = "95% CI")) +
  scale_color_manual(name = "Legend", values = c("#666666", "#1B9E77")) +
  guides(colour = guide_legend(override.aes = list(linetype = c("solid", "blank"), 
                                                   shape = c(NA, 15))))
sp_ts3

编辑:要显示填充的矩形,我们需要使用geom_rect 而不是annotate

sp_ts3 + 
    geom_rect(aes(fill = "First"), xmin = -Inf, xmax = 2, ymin = -Inf, ymax = Inf, 
           alpha = 0.1) +
    geom_rect(aes(fill = "Second"), xmin = 2, xmax = Inf, ymin = -Inf, ymax = Inf, 
           alpha = 0.1) +
    scale_fill_manual(name = "Group", 
                      values = c(`First` = "bisque", `Second` = "cornflowerblue")) +
    guides(fill = guide_legend(override.aes= list(alpha = 0.6)))

reprex package (v0.2.0) 于 2018 年 4 月 4 日创建。

【讨论】:

  • 谢谢Tung,这将用于手段和错误栏。你也知道如何合并矩形吗?
  • 对不起,还有一个问题。均值的正方形现在也被着色了,因为矩形被放置在顶部。您知道我如何配置图表,以使均方仍然是黑色(或在您的情况下为绿色)并位于矩形顶部吗?所以他们有纯色?
  • 好吧,没关系,我找到了解决方案:只需将要绘制在顶部的代码放在要绘制在底部的代码下方即可。所以:geom_rect 先于 geom_point
猜你喜欢
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 2016-12-29
  • 2012-08-31
  • 2020-08-18
  • 2012-09-01
  • 1970-01-01
  • 2021-10-29
相关资源
最近更新 更多