Search code examples
rdataframeggplot2plotlubridate

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


I am trying to plot multiple location data using facet_wrap functionality of ggplot. I am having trouble in creating the legends (95% confidence interval is completely missed). Below is my code and I would appreciate any suggestion.

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"))

Solution

  • enter image description here

    Your fill is outside the aes function, so it cannot appear in the legend.

    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')))