Одна метка на оси y, а вторая оставляет ее вверху. Использование facet_wrap с 2 переменными

#r #ggplot2 #facet #facet-wrap

#r #ggplot2 #фасет #facet-wrap

Вопрос:

Я хочу добавить ось y для каждой грани. facet_wrap(scales="free_y") не делает то, что я хочу, потому switch=y что перемещает обе переменные, а я хочу переместить только одну.

  • Можно ли с этим что-то сделать?
  • Знаете ли вы, есть ли другой способ получить ось для каждой грани таким же образом scales="free_y" ?.

Спасибо, что уделили время.

график, который я хочу

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

Ответ №1:

Возможно, это то, что вы ищете. Один из способов получить ось y для каждой грани — использовать пэчворк, т.Е. Создавать Отдельные графики для каждого столбца сетки и склеивать графики вместе. Один недостаток: обе оси x помечаются:

 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") amp;
  theme(legend.position='bottom')