简体   繁体   English

如何在两个图表ggplot 2上对齐y轴,以使它们具有相同的范围和增量

[英]How to align y-axis on two graphs ggplot 2 so that they have the same range and increments

I used ggarrange to make a figure with two panels.我用 ggarrange 制作了一个有两个面板的人物。 I want the two panels to have the same y-axis range and increments.我希望两个面板具有相同的 y 轴范围和增量。 Even with using scale_y_continuous, I can't seem to get them to line up correctly.即使使用 scale_y_continuous,我似乎也无法让它们正确排列。 Plot Plot

Here's the code I've been working with:这是我一直在使用的代码:

ei<- ggplot(data=ingestion, aes(x=Species, y=Numberofparticlesingested, colour=factor(Condition), shape=factor(Condition)))
ei1<- ei + scale_shape_manual(values = c(16,1),name="Condtion",breaks=c("Healthy","Bleached"),labels=c("Healthy","Bleached"))+ scale_color_manual(values=c("black", "grey"),name="Condtion",breaks=c("Healthy","Bleached"),labels=c("Healthy","Bleached"))+ 
  stat_summary(fun.y=mean, geom="point",size=2, position=position_dodge(width =0.90)) + 
  stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom= 
                 "errorbar", position=position_dodge(width=0.90), width=0.2) +scale_y_continuous(limits = c(0, 60))+
  labs(x="Species", y="Average number of particles") + 
  theme_bw()+ theme(legend.justification = "top")+ theme(legend.key = element_rect(fill = "white", colour = "black"))+ theme(text = element_text(size = 14, family = "sans"))

ei2<- ei1 + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
  theme(strip.background = element_rect(fill="white"))+ theme(
    axis.text.x = element_text(size = 14, vjust = -0.2, color = "black", family = "sans")) + theme(axis.text.y=element_text(size=14, hjust=1, color="black", family = "sans")) 
e<- ggplot(data=egestioncumulativedata, aes(x=Time, y=Numberofparticles, colour=factor(Condition), shape=factor(Condition)))
e1<- e + scale_shape_manual(values = c(16,1),name="Condtion",breaks=c("Healthy","Bleached"))+ scale_color_manual(values=c("black", "grey"),name="Condtion",breaks=c("Healthy","Bleached"))+
  facet_grid(~Species) + 
  stat_summary(fun.y=mean, geom="point", size = 2, position=position_dodge(width =0.90)) + stat_summary(fun.y=mean, geom="line", size = 0.8, position=position_dodge(width =0.90))+
  stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom= 
                 "errorbar", position=position_dodge(width=0.90), width=0.52) + scale_x_discrete(limits=c(6, 12, 18, 24, 30, 36, 42, 48)) + scale_y_continuous(limits = c(0, 60)) +
  labs(x=expression(Time~ (Hours)), y=expression(Average ~number ~of ~particles)) + 
  theme_bw()+ theme(legend.justification = "top")+ theme(legend.key = element_rect(fill = "white", colour = "black"))+ theme(text = element_text(size = 14, family = "sans"))

e2<- e1 + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
  theme(strip.background = element_rect(fill="white"))+ scale_fill_discrete(breaks=c("AI","PV"))+ theme(
    axis.text.x = element_text(size = 14, vjust = -0.25, color = "black", family = "sans")) + theme(axis.text.y=element_text(size=14, hjust=1, color="black", family = "sans")) 
library(ggpubr)
figure6<-ggarrange(ei2, 
                   e2+ theme(
                     axis.title.y = element_blank() ),
                   labels= c("a", "b"),
                   nrow = 1,ncol = 2, 
                   align = "hv", 
                   common.legend = TRUE, legend = "right")

Using the breaks and labels arguments in addition to the limits argument in scale_y_continuous should give you better control of the axes除了 scale_y_continuous 中的limits参数之外,使用breakslabels scale_y_continuous应该可以更好地控制轴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM