如何使用 facet_wrap 在 R 中绘制多个位置点?

how to use facet_wrap to ggplot multiple location point in R?

我正在尝试使用 ggplotfacet_wrap 功能来 plot 多个位置数据。我在创建 legends 时遇到问题(完全错过了 95% 的置信区间)。以下是我的代码,如有任何建议,我将不胜感激。

library(ggplot2)
library(lubridate)

set.seed(123)

DF1 <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "1 month"),
                  Ob = runif(60,1,5), L95 =runif(60, 0,4), U95 = runif(60,2,7), Sim = runif(60,1,5),
                  Loc = rep("Upstream", 60))

DF2 <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "1 month"),
                  Ob = runif(60,1,5), L95 =runif(60, 0,4), U95 = runif(60,2,7), Sim = runif(60,1,5),
                  Loc = rep("Downstream", 60))

DF <- dplyr::bind_rows(DF1,DF2)

DF$Loc <- factor(DF$Loc, levels = c("Upstream","Downstream"))


ggplot(DF, aes(x = Date))+
  geom_ribbon(aes(ymin = L95, ymax = U95), fill = "grey30", alpha = 0.4)+
  geom_line(aes(y = Ob, color = "blue"), size = 1 )+
  geom_line(aes(y = Sim, color = "black"), size =  1, linetype = "dashed")+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
  facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
  theme_bw()+
  scale_color_identity(guide = "legend", breaks = c("grey30", "blue", "black"),
                       labels = c("95% confidence bound", "Observation","Simulation"))

下面是我的做法。

首先,您使用的是 fill 作为功能区,而不是 color。其次,您需要在 aes 中实际映射 fill,而不仅仅是将其设置在 aes 之外。然后我会在 aes 调用中给出您想要的名称,并使用 scale_*_manual 设置您想要的值:

ggplot(DF, aes(x = Date))+
  geom_ribbon(aes(ymin = L95, ymax = U95, fill = "95% confidence bound"), alpha = 0.4)+
  geom_line(aes(y = Ob, color = "Observation"), size = 1 )+
  geom_line(aes(y = Sim, color = "Simulation"), size =  1, linetype = "dashed")+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
  facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
  theme_bw()+
  scale_color_manual(values = c('blue', 'black'), name = NULL) +
  scale_fill_manual(values = 'grey30', name = NULL)

有很多有效的方法来解决这个问题,但有多少人这样做。

您的填充在aes函数之外,因此无法出现在图例中。

ggplot(DF, aes(x = Date))+
  geom_ribbon(aes(ymin = L95, ymax = U95, color = "grey30"), fill = "grey30", alpha = 0.4)+
  geom_line(aes(y = Ob, color = "blue"), size = 1 )+
  geom_line(aes(y = Sim, color = "black"), size =  1, linetype = "dashed")+
  geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
  facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
  theme_bw()+
  scale_color_identity(guide = "legend", breaks = c("grey30", "blue", "black"),
                       labels = c("95% confidence bound", "Observation","Simulation"))
  ggplot(DF, aes(x = Date))+
    geom_ribbon(aes(ymin = L95, ymax = U95, fill = "grey30"), alpha = 0.4)+
    geom_line(aes(y = Ob, color = "blue"), size = 1 )+
    geom_line(aes(y = Sim, color = "black"), size =  1, linetype = "dashed")+
    geom_vline(xintercept = as.Date("2004-12-01"),color = "red", size = 1.30)+
    facet_wrap(~ Loc, ncol = 1, scales = "free_y")+ 
    theme_bw()+
    scale_color_identity(guide = "legend", breaks = c( "blue", "black"),
                         labels = c( "Observation","Simulation"),
                         name = 'Legend')+
    scale_fill_identity(guide = "legend", labels = c("95% confidence bound"), 
                        name=NULL)+
    theme(legend.spacing.y = unit(-0.2, "cm"))+
    theme(legend.title = element_text(margin=margin(b = 0.4, unit='cm')))