Home > Software engineering >  ggplot2: add two groups to one facet
ggplot2: add two groups to one facet

Time:12-21

I have a dataframe with of annual temperature time series from two locations (i.e., Site 1 & Site 2). The three temperature variables are:

  1. Air temperature
  2. Water temperature
  3. Difference = Air - Water

I would like to produce a four-panel figure where the top and bottom rows are Site 1 and Site 2 respectively, the left column displays Air and Water and the right column shows Difference. Is there a way to do this using facet_wrap() or facet_grid()?

Example Data

library(data.table)
library(dplyr)
library(ggplot2)

set.seed(321)

# Create the example air and water temperature time series
df1 <- data.frame(matrix(ncol = 4, nrow = 365*4))
colnames(df1)[1:4] <- c("Location","Variable", "Date", "Temperature")
df1[1:730,1] <- "Site 1"
df1[731:NROW(df1),1] <- "Site 2"
df1[c(1:365,731:1095),2] <- "Air"
df1[c(366:730,1096:NROW(df1)),2] <- "Water"
df1$Date <- rep(seq.Date(as.Date("2021-01-01"),as.Date("2021-12-31"),"1 day"),4)
df1$noise <- rep(runif(365),4)
df1$t <- rep(seq(0,1*pi,,365),4)
for (i in 1:NROW(df1)) {
  df1$Temperature[1:365] <- 20*sin(df1$t) df1$noise*8
  df1$Temperature[365:730] <- 17*sin(df1$t) df1$noise*2
  df1$Temperature[731:1095] <- 25*sin(df1$t) df1$noise*6
  df1$Temperature[1096:NROW(df1)] <- 18*sin(df1$t) df1$noise*1.5
}

# Take the difference between air and water temperature
df1 <- df1[,1:4]
site1 <- df1[df1$Location == 'Site 1',]
site1 <- site1 %>%
  tidyr::pivot_wider(names_from = Variable, values_from = Temperature) %>%
  mutate(Difference = Air - Water) %>%
  tidyr::pivot_longer(cols = c('Water','Air','Difference'),
                      names_to = 'Variable',
                      values_to = 'Temperature')
site2 <- df1[df1$Location == 'Site 2',]
site2 <- site2 %>%
  tidyr::pivot_wider(names_from = Variable, values_from = Temperature) %>%
  mutate(Difference = Air - Water) %>%
  tidyr::pivot_longer(cols = c('Water','Air','Difference'),
                      names_to = 'Variable',
                      values_to = 'Temperature')

# Recombine data from site 1 and site 2 for final dataset
df1 <- rbind(site1,site2)

This is an example of what I am looking for, however instead of having a six-panel figure, I would like Air and Water displayed together, creating a four-panel figure.

df1 %>%
  ggplot()  
  geom_line(aes(x = Date, y = Temperature, group = Variable, color = Variable))  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
        axis.text.y = element_text(size = 14, color = "black"))  
  facet_grid(Location~Variable)

CodePudding user response:

Try creating a new variable that groups "Air" and "Water" observatiopns, and specifying it to the facet:

df1 %>% 
mutate(var_air_water = ## Here is the new variable
   if_else(Variable %in% c("Air", "Water"), 
   true = "Air & Water", 
   false = Variable)) %>% 
ggplot()  
geom_line(aes(x = Date, y = Temperature, group = Variable, color = Variable))  
theme_bw()  
theme(panel.grid = element_blank(),
      text = element_text(size = 16),
      axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
      axis.text.y = element_text(size = 14, color = "black"))  
facet_grid(Location~var_air_water)

enter image description here

  • Related