我有3个比例尺不同的地块,我想按0对齐它们。
我尝试在align
中玩plot_grid
,但没有结果。
三个不同图的数据框:
1
higherperiod=structure(list(phen = c("GS0", "GS1", "GS2", "GS3", "H", "GS0",
"GS1", "GS2", "GS3", "H", "GS0", "GS1", "GS2", "GS3", "H"), var = c("mz33_flux",
"mz33_flux", "mz33_flux", "mz33_flux", "mz33_flux", "mz45_flux",
"mz45_flux", "mz45_flux", "mz45_flux", "mz45_flux", "mz137_flux",
"mz137_flux", "mz137_flux", "mz137_flux", "mz137_flux"), value = c(-0.0201807203084833,
0.00699548966597077, 0.2982090471597, 0.763140160427808, 0.0115715254237288,
-0.00141967461669506, 0.0216389158168574, 0.152685557877813,
0.0861748924731184, 0.00783050847457625, -0.0568113340318524,
-0.0283260888657648, 0.0584874208715596, -0.0014408875, -0.0199876076811594
), err = c(0.00891612382048675, 0.00865982415221607, 0.0261407868072828,
0.150893410160645, 0.000915642610907682, 0.00313669333464331,
0.00539139343017679, 0.0109634891854869, 0.0244123650545713,
0.000586325133571454, 0.0618423495252825, 0.0290907608410913,
0.0405039109957796, 0.0135552560862062, 0.0016406585376453),
tot = c(0.104295388186188, NA, NA, NA, NA, 0.0462282825845532,
NA, NA, NA, NA, -0.0152536834035828, NA, NA, NA, NA)), row.names = c(6L,
7L, 8L, 9L, 10L, 21L, 22L, 23L, 24L, 25L, 106L, 107L, 108L, 109L,
110L), class = "data.frame")
2
middleperiod=structure(list(phen = c("GS0", "GS1", "GS2", "GS3", "H", "GS0",
"GS1", "GS2", "GS3", "H", "GS0", "GS1", "GS2", "GS3", "H", "GS0",
"GS1", "GS2", "GS3", "H"), var = c("mz47_flux", "mz47_flux",
"mz47_flux", "mz47_flux", "mz47_flux", "mz59_flux", "mz59_flux",
"mz59_flux", "mz59_flux", "mz59_flux", "mz61_flux", "mz61_flux",
"mz61_flux", "mz61_flux", "mz61_flux", "mz69_flux", "mz69_flux",
"mz69_flux", "mz69_flux", "mz69_flux"), value = c(0.00321274882747069,
-0.000643201705862667, 0.0243593290460879, 0.0349870967741935,
0.00875693502824857, 0.00482035695142378, -0.00353965563598759,
0.0275966956055734, -0.00348043010752689, -0.000620290395480228,
-0.000315081786912752, 0.00159501955440415, 0.0094602285101822,
0.0286145161290322, 0.000502053418803419, 0.00148408262008734,
0.00234769254658385, 0.00983096248660236, 0.0150233333333333,
-0.00391782485875705), err = c(0.00263879547828413, 0.0028541449123107,
0.00811068959122311, 0.0220314197658351, 0.00079136625093948,
0.00296923961006679, 0.0052170248712633, 0.00508938680657341,
0.0204694740104851, 0.000721653962440168, 0.00129596725971262,
0.00199557392661037, 0.00468345194254694, 0.009973392065427,
0.000355559839113475, 0.00116104692269852, 0.00305328555952633,
0.00184122489127936, 0.00907852219101064, 0.000270910235959759
), tot = c(0.00969127634163261, NA, NA, NA, NA, 0.00676876895687063,
NA, NA, NA, NA, 0.00393784592257415, NA, NA, NA, NA, 0.00334178608430675,
NA, NA, NA, NA)), row.names = 26:45, class = "data.frame")
3
lowerperiod=structure(list(phen = c("GS0", "GS1", "GS2", "GS3", "H", "GS0",
"GS1", "GS2", "GS3", "H"), var = c("mz99_flux", "mz99_flux",
"mz99_flux", "mz99_flux", "mz99_flux", "mz101_flux", "mz101_flux",
"mz101_flux", "mz101_flux", "mz101_flux"), value = c(-0.000620104717775904,
-0.0010108935483871, 0.00111282528735632, 0.003705125, -0.00306231725146199,
-0.000496990050251256, -0.00158667942088935, 0.00275467202572347,
0.00294259677419355, -0.000237937853107344), err = c(0.000530108561256672,
0.000665450652678023, 0.00102123430149931, 0.0033050927052054,
0.000138258080980254, 0.000524645790673878, 0.00212209870609353,
0.000619468013983048, 0.00254127441214106, 9.71415042719002e-05
), tot = c(-0.000670965097831834, NA, NA, NA, NA, 0.000205929508525576,
NA, NA, NA, NA)), row.names = 76:85, class = "data.frame")
三个情节的代码,每个情节已经使用facet_grid
:
1
voclabh=c(mz137_flux="Monoterpenes",mz33_flux="Methanol",mz45_flux="Acetaldehyde")
high=ggplot(higherperiod,aes(x=phen,y=value,group=1))+
geom_bar(aes(x='GS2', y = tot,fill='Whole growing season'),colour="black",
stat="identity",position='dodge', width = 5) +
geom_errorbar(aes(ymin=value-err, ymax=value+err), width=0.2,
position=position_dodge(0.9),colour="black")+
geom_line(aes(colour="Single phenological stage"), linetype="solid",size = 1)+
geom_point(aes(colour="Single phenological stage"),shape = 21, fill='white',size = 2)+
facet_grid(~var,scales="free_y",labeller = labeller(var=voclabh),switch = "x")+
scale_colour_manual(values = c("black"))+
scale_fill_manual(values = c('darkolivegreen4'))+
scale_x_discrete(name = "Phenological stages") +
scale_y_continuous(name = expression(bold(atop("Mean Flux",(nmol~m^bold("-2")~s^bold("-1"))))),breaks = seq(-0.25, 1 ,0.25),
limits=c(-0.25, 1))+
theme_classic() +theme(legend.position = 'none',
legend.title = element_blank())+
theme(plot.title = element_text(size = 16, family = "Tahoma", face = "bold"),
text = element_text(size = 14, family = "Tahoma"),
axis.title.y = element_text(face="bold",size=16),
axis.text.y = element_text(color="black",size=16),
axis.title.x=element_blank(), axis.text.x=element_blank(),axis.ticks.x=element_blank(),
strip.text.x = element_text(size = 13.5, color = "black",face="bold"),
strip.background = element_blank(), panel.spacing = unit(0, "lines"),
strip.placement = "outside",
legend.text = element_text(color = "black", size = 16,face="bold"))+
geom_hline(yintercept=0, linetype="solid", color = "black",size=1,alpha=0.9)
2
voclabm2=c(mz47_flux="Formic Acid",mz59_flux="Acetone",mz61_flux="Acetic acid",mz69_flux="Isoprene")
mid=ggplot(middleperiod,aes(x=phen,y=value,group=1))+
geom_bar(aes(x='GS2', y = tot,fill='Whole growing season'),colour="black",
stat="identity",position='dodge', width = 5) +
geom_errorbar(aes(ymin=value-err, ymax=value+err), width=0.2,
position=position_dodge(0.9),colour="black")+
geom_line(aes(colour="Single phenological stage"), linetype="solid",size = 1)+
geom_point(aes(colour="Single phenological stage"),shape = 21, fill='white',size = 2)+
facet_grid(~var,scales="free_y",labeller = labeller(var=voclabm2),switch = "x")+
scale_colour_manual(values = c("black"))+
scale_fill_manual(values = c('darkolivegreen4'))+
scale_x_discrete(name = "Phenological stages") +
scale_y_continuous(name = expression(bold(Flux~(nmol~m^bold("-2")~s^bold("-1")))),breaks = seq(-0.06, 0.06 ,0.03),
limits=c(-0.06, 0.06))+
theme_classic() +theme(legend.position = 'none')+
theme(plot.title = element_text(size = 16, family = "Tahoma", face = "bold"),
text = element_text(size = 14, family = "Tahoma"),
axis.title.y = element_blank(),
axis.text.y = element_text(color="black",size=16),
axis.title.x=element_blank(), axis.text.x=element_blank(),axis.ticks.x=element_blank(),
strip.text.x = element_text(size = 13.5, color = "black",face="bold"),
strip.background = element_blank(), panel.spacing = unit(0, "lines"),
strip.placement = "outside",
legend.text = element_text(color = "black", size = 16))+
geom_hline(yintercept=0, linetype="solid", color = "black",size=1,alpha=0.9)
3
voclabl=c(mz99_flux="Hexenal",mz101_flux="Hexenol")
low=ggplot(lowerperiod,aes(x=phen,y=value,group=1))+
geom_bar(aes(x='GS2', y = tot,fill='Whole growing season'),colour="black",
stat="identity",position='dodge', width = 5) +
geom_errorbar(aes(ymin=value-err, ymax=value+err), width=0.2,
position=position_dodge(0.9),colour="black")+
geom_line(aes(colour="Single phenological stage"), linetype="solid",size = 1)+
geom_point(aes(colour="Single phenological stage"),shape = 21, fill='white',size = 2)+
facet_grid(~var,scales="free_y",labeller = labeller(var=voclabl),switch = "x")+
scale_colour_manual(values = c("black"))+
scale_fill_manual(values = c('darkolivegreen4'))+
scale_x_discrete(name = "Phenological stages") +
scale_y_continuous(name = expression(bold(Flux~(nmol~m^bold("-2")~s^bold("-1")))),breaks = seq(-0.005, 0.0075,0.0025),
limits=c(-0.005, 0.0075),labels=sprintf("%.2f",y))+
theme_classic() +theme(legend.position = 'none')+
theme(plot.title = element_text(size = 16, family = "Tahoma", face = "bold"),
text = element_text(size = 14, family = "Tahoma"),
axis.title.y = element_blank(),
axis.text.y = element_text(color="black",size=16),
axis.title.x=element_blank(), axis.text.x=element_blank(),axis.ticks.x=element_blank(),
strip.text.x = element_text(size = 13.5, color = "black",face="bold"),
strip.background = element_blank(), panel.spacing = unit(0, "lines"),
strip.placement = "outside",
legend.text = element_text(color = "black", size = 16))+
geom_hline(yintercept=0, linetype="solid", color = "black",size=1,alpha=0.9)
将它们组合在一起的代码:
legendtot = get_legend(high +
theme(legend.box = "horizontal",
legend.position = c(1.55, 0.4)))
totmenaplot = plot_grid(high, mid, low, legendtot,
ncol = 3, align = "hv",
rel_widths = c(1,1,1), rel_heights = c(12,1,1))
如图所示,在3个不同图的y轴上的零位于不同的“高度”。
我想将所有零对齐在同一高度。是否可以在每个y轴上使用不同的比例尺?
答案 0 :(得分:1)
获得理想外观的一种方法是修改每个图的y轴范围,以使所有三个图具有相同的比例长度,分别分配给负象限和正象限。
# examine y-axis limits for each plot
high$scales$scales[[4]]$limits # -0.25, 1 ratio of negative to positive is 1:4
mid$scales$scales[[4]]$limits # -0.06, 0.06 ratio of negative to positive is 1:1
low$scales$scales[[4]]$limits # -0.0050, 0.0075 ratio of negative to positive is 1:1.5
我们可以将比例标准化为1:4,例如:
mid$scales$scales[[4]]$limits[2] <- abs(mid$scales$scales[[4]]$limits[1]) * 4
low$scales$scales[[4]]$limits[2] <- abs(low$scales$scales[[4]]$limits[1]) * 4
从之前重新运行plot_grid
行:
plot_grid(high, mid, low, legendtot,
ncol = 3, align = "hv",
rel_widths = c(1, 1, 1),
rel_heights = c(12, 1, 1))