一个标签在 y 轴上,第二个标签留在顶部。使用 facet_wrap 和 2 个变量
One label on the y axis and the second one leave it at the top. Using facet_wrap with 2 variables
我想在每个方面添加一个 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-axis 的一种方法是使用拼凑,即为网格的每一列制作单独的图并将图粘合在一起。一个缺点:x-axis 都被标记为:
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')
我想在每个方面添加一个 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-axis 的一种方法是使用拼凑,即为网格的每一列制作单独的图并将图粘合在一起。一个缺点:x-axis 都被标记为:
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')