Home > Software design >  How to align y-axis on two graphs ggplot 2 so that they have the same range and increments
How to align y-axis on two graphs ggplot 2 so that they have the same range and increments

Time:12-21

I used ggarrange to make a figure with two panels. I want the two panels to have the same y-axis range and increments. Even with using scale_y_continuous, I can't seem to get them to line up correctly. 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")

CodePudding user response:

Using the breaks and labels arguments in addition to the limits argument in scale_y_continuous should give you better control of the axes

  • Related