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

如何使用facet_wrap在R中绘制多个位置点?,r,dataframe,ggplot2,plot,lubridate,R,Dataframe,Ggplot2,Plot,Lubridate,我正在尝试使用ggplot的facet_wrap功能绘制多个位置数据。我在创建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 = &qu

我正在尝试使用ggplot的facet_wrap功能绘制多个位置数据。我在创建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"))

以下是我将如何做到这一点

首先,您对功能区使用的是填充,而不是颜色。其次,您需要实际映射aes中的填充,而不仅仅是将其设置在aes之外。然后,我会在aes调用中给出您想要的名称,并使用scale_*_手动设置您想要的值:

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中的填充,而不仅仅是将其设置在aes之外。然后,我会在aes调用中给出您想要的名称,并使用scale_*_手动设置您想要的值:

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"))
您的填充不在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"))

我在移除图例周围的框时遇到问题-我尝试了elegend.key=element\u rectcolor=NA,fill=NA,legend.box.background=element\u blank,但它不起作用-对此有什么想法吗?我不太明白!你在找这样的东西吗?是的,但它在这里不起作用-不知道为什么?你能移除图例周围的方框吗?没错,这似乎不起作用。见我在回答中的编辑谢谢你@Liman。有没有办法只向第一个方面添加注释?我尝试了注释Geom=Text,x=as.Date2000-01-01,y=5,label=Calibration,但这在两个方面都添加了注释。我很难删除图例周围的框-我尝试了elegend.key=element\u rectcolour=NA,fill=NA,legend.box.background=element\u blank,但它不起作用-对此有任何想法吗?我不太明白!你在找这样的东西吗?是的,但它在这里不起作用-不知道为什么?你能移除图例周围的方框吗?没错,这似乎不起作用。见我在回答中的编辑谢谢你@Liman。有没有办法只向第一个方面添加注释?我尝试注释geom=Text,x=as.Date2000-01-01,y=5,label=Calibration,但这在两个方面都添加了注释。