Y 축에 하나의 레이블이 있고 두 번째 레이블은 맨 위에 둡니다. 2 개의 변수와 함께 facet_wrap 사용

RobertoSupe

각 패싯에 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"))
스테판

아마도 이것은 당신이 찾고있는 것입니다. 각 패싯에서 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") &
  theme(legend.position='bottom')

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관