图例ggplot图没有显示平均值,错误栏和彩色矩形

时间:2018-04-04 06:23:52

标签: r ggplot2 legend

我一直在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())

1 个答案:

答案 0 :(得分:4)

以下是展示图例的一种方式(取自此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-04-04。