Search code examples
rggplot2facetfacet-wrap

One label on the y axis and the second one leave it at the top. Using facet_wrap with 2 variables


I want to add a y-axis on each facet. facet_wrap(scales="free_y") doesn't do what I want because switch=y moves both variables and I want to move only one.

  • Is it possible to do something about it?
  • Do you know if there is another way to get the axis on each facet the same way scales="free_y" does?.

Thank you for the time.

the plot that I want

#code for figure 
 ggplot(df,aes(factor(Year),Value,colour=Model,group=Model))+
geom_line(size=0.6)+
  scale_colour_manual(values = c("Black","blue","darkgreen","red"))+
  #scale_colour_manual(values = c("Black","#00539CFF","#006B38FF","#E94B3CFF"))+
scale_x_discrete(labels=c(1981,2005,2026,2050,2075,2100),breaks=factor(c(1981,2005,2026,2050,2075,2100)),expand = c(0.04,0.04))+
  facet_grid(Variable~Location,scales = "free_y",switch = "y", # flip the facet labels along the y axis from the right side to the left
             labeller = as_labeller(c(CDD = "CDD (Days)",CWD="CWD (Days)",R20="R20 (Days)",R50= "R50 (Days)",R95P="R95P (mm)",R99P="R99p (mm)",
                                      RX5DAY="RX5DAY (mm)",SDII="SDII (mm/day)",Bimodal="Bimodal",Unimodal="Unimodal")))+
  labs(x="Year",y=element_blank(),colour=element_blank())+
  guides(colour = guide_legend(override.aes = list(size = 2)))+
  theme_bw(base_size = 15)+
  theme(strip.background = element_blank(), # remove the background
        strip.placement = "outside",legend.position = "bottom",
        legend.text = element_text(size = 15),
        axis.text = element_text(colour="black"))#,
        panel.spacing.x = unit(-0.90, "lines"),
        panel.spacing.y = unit(-0.80, "lines"))```

df<-structure(list(Year = c(2091, 2091, 2091, 2092, 2092, 2092, 2093, 
2093, 2093, 2094, 2094, 2094, 2095, 2095, 2095, 2096, 2096, 2096, 
2097, 2097, 2097, 2098, 2098, 2098, 2099, 2099, 2099, 2100, 2100, 
2100, 2091, 2091, 2091, 2092, 2092, 2092, 2093, 2093, 2093, 2094, 
2094, 2094, 2095, 2095, 2095, 2096, 2096, 2096, 2097, 2097, 2097, 
2098, 2098, 2098, 2099, 2099, 2099, 2100, 2100, 2100), Variable = c("CDD", 
"CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", 
"CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", 
"CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", 
"CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", 
"CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", 
"CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", "CDD", 
"CDD", "CDD", "CDD", "CDD", "CDD"), Location = c("Unimodal", 
"Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", 
"Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", 
"Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", 
"Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", 
"Unimodal", "Unimodal", "Unimodal", "Unimodal", "Unimodal", "Bimodal", 
"Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal", 
"Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal", 
"Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal", 
"Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal", 
"Bimodal", "Bimodal", "Bimodal", "Bimodal", "Bimodal"), Model = c("RCP2.6", 
"RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", 
"RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", 
"RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", 
"RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", 
"RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", 
"RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", 
"RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", 
"RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", "RCP2.6", "RCP4.5", "RCP8.5", 
"RCP2.6", "RCP4.5", "RCP8.5"), Value = c(188.47, 184.03, 199.62, 
172.65, 204.21, 186.82, 179.75, 196.57, 198.9, 185.33, 193.11, 
197.07, 174.92, 200.37, 210.65, 176.48, 200.01, 203.91, 170.33, 
200.67, 192.44, 164.03, 200.35, 188.92, 169, 208.69, 196.54, 
183.62, 203.8, 201.36, 135.92, 141.75, 145.53, 111.85, 145.43, 
160.49, 127.08, 144.34, 155.95, 127.36, 154.24, 155.37, 127.92, 
135.34, 155.45, 139.08, 146.16, 160.27, 129.21, 151.82, 145.07, 
108.86, 161.75, 146.93, 134.71, 155.76, 157.29, 117.42, 155.58, 
150.69)), row.names = c(NA, -60L), class = c("tbl_df", "tbl", 
"data.frame"))

Solution

  • Maybe this is what you are looking for. One way to get a y-axis on each facet is to make use of patchwork, i.e. make separate plots for each column of the grid and glue the plots together. One drawback: Both x-axis get labelled:

    library(ggplot2)
    library(dplyr)
    library(patchwork)
    
    make_plot <- function(d, switch = NULL) {
      ggplot(d, aes(factor(Year), Value, colour = Model, group = Model)) +
        geom_line(size = 0.6) +
        scale_colour_manual(values = c("Black", "blue", "darkgreen", "red")) +
        scale_x_discrete(labels = c(1981, 2005, 2026, 2050, 2075, 2100), breaks = factor(c(1981, 2005, 2026, 2050, 2075, 2100)), expand = c(0.04, 0.04)) +
        facet_grid(Variable ~ Location,
                   scales = "free_y", 
                   switch = switch, # flip the facet labels along the y axis from the right side to the left
                   labeller = as_labeller(c(
                     CDD = "CDD (Days)", CWD = "CWD (Days)", R20 = "R20 (Days)", R50 = "R50 (Days)", R95P = "R95P (mm)", R99P = "R99p (mm)",
                     RX5DAY = "RX5DAY (mm)", SDII = "SDII (mm/day)", Bimodal = "Bimodal", Unimodal = "Unimodal"
                   ))
        ) +
        guides(colour = guide_legend(override.aes = list(size = 2))) +
        labs(x = "Year", y = NULL, colour = NULL) +
        theme_bw(base_size = 15) +
        theme(
          strip.background = element_blank(), # remove the background
          strip.placement = "outside", legend.position = "bottom",
          legend.text = element_text(size = 15),
          axis.text = element_text(colour = "black")
        )  
    }
    
    p1 <- make_plot(filter(df, Location == "Bimodal"), switch = "y")
    p2 <- make_plot(filter(df, Location == "Unimodal"), switch = "y")
    
    
    p1 + p2 + plot_layout(guides = "collect") &
      theme(legend.position='bottom')