简体   繁体   中英

Multiple plots on the same figure using facet_wrap functionality of ggplot in R?

I am trying to add accumulated values of a particular years for multiple locations on top of the Figure that has statistics for those locations. Below is a sample code (taken from a solution propose to one of my previous question).

library(tidyverse)
library(lubridate)
library(dplyr)
library(tidyr)

mydate <- as.data.frame(seq(as.Date("2000-01-01"), to= as.Date("2019-12-31"), by="day"))
    colnames(mydate) <- "Date"
    Data <- data.frame(A = runif(7305,0,10), 
                       J = runif(7305,0,8), 
                       X = runif(7305,0,12), 
                       Z = runif(7305,0,10))

    DF <- data.frame(mydate, Data)

    Data_Statistics <- DF %>% mutate(Year = year(Date), Month = month(Date)) %>%
      pivot_longer(-c(Date,Year,Month), names_to = "variable", values_to = "values") %>% 
      filter(between(Month,5,10)) %>% 
      group_by(Year, variable) %>% 
      mutate(Cumulative = cumsum(values)) %>%
      mutate(NewDate = ymd(paste("2020", Month,day(Date), sep = "-"))) %>%
      ungroup() %>%
      group_by(variable, NewDate) %>%
      summarise(Median = median(Cumulative),
                Maximum = max(Cumulative),
                Minimum = min(Cumulative),
                Upper = quantile(Cumulative,0.75),
                Lower = quantile(Cumulative, 0.25))

I wanted to extract data for the year 2019 out of Data_Statistics , however, failed to do- I do not want statistics for the year 2019 but accumulated values along the period of my interest (May to October which corresponds to months 5 - 10)

 Data_2019 <- DF %>% mutate(Year = year(Date), Month = month(Date)) %>%
  pivot_longer(-c(Date,Year,Month), names_to = "variable", values_to = "values") %>% 
  filter(between(Month,5,10)) %>%
  filter(Year == 2019) %>% 
  group_by(Year, variable) %>% 
  mutate(Cumulative = cumsum(values)) 

Plotting the Data_Statistics using facet_wrap functionality of ggplot with the following sample code gave me attached Figure.

Data_Statistics %>% pivot_longer(cols = c(Median, Minimum,Maximum), names_to = "Statistic",values_to = "Value") %>%
  ggplot(aes(x = NewDate))+
  geom_ribbon(aes(ymin = Lower, ymax = Upper, fill = "Upper / Lower"), alpha =0.5)+
  geom_line(aes(y = Value, color = Statistic, linetype = Statistic, size = Statistic))+
  facet_wrap(~variable, scales = "free")+
  scale_x_date(date_labels = "%b", date_breaks = "month", name = "Month")+
  ylab("Daily Cumulative Precipitation (mm)")+
  scale_size_manual(values = c(1.5,1,1.5))+
  scale_linetype_manual(values = c("dashed","solid","dashed"))+
  scale_color_manual(values = c("red","darkblue","black"))+
  scale_fill_manual(values = "cyan", name = "")

在此处输入图片说明

My ultimate goal

I want to add the year 2019 data on top of the figure for its respective facets (ie, another geom_line) to see what we have in comparison to the statistics of all the previous years. Thank you.

If you want to overlay each facet with 2019 data, you add a new geom_line function to your ggplot. You need to first manipulate the 2019 data like you did for the total data ahead of this:

Data_2019_plot <- Data_2019  %>% 
  pivot_longer(cols = c(Median, Minimum,Maximum), names_to = "Statistic",values_to = "Value")

Now at the end of your ggplot sequence you add

+ geom_line(data = Data_2019_plot, 
            aes(y = Value, color = Statistic, linetype = Statistic, size = Statistic))

and you get the following plot: 在此处输入图片说明

With your sample data, the 2019 lines overlap quite a lot so they don't look very clear. You may want to set a specific color for 2019 rather than setting it to the aesthetic mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM