How to plot common y-axis text for combined figure (ggplot2)

Viewed 43

I'm a beginner in R (and ggplot2).

I'm trying to make 1x3 figure, and scale of y axis is same for all three figures.

Also, I want to remove text and title of y axis for second and third plot.

However, I realized that width of first figure become narrow than second & third plot when removing axis text and title.

So, my question is, is there any way to make a common y-axis text? (so that all three figures have same width)

Here's my example code and figure, thanks.enter image description here

library(ggplot2)

df<-data.frame(x=1:5, y1=runif(5), y2=runif(5), y3=runif(5))

p1<-ggplot()+
  geom_line(data=df, aes(x=x, y=y1))+
  coord_cartesian(ylim = c(0, 1))

p2<-ggplot()+
  geom_line(data=df, aes(x=x, y=y2))+
  theme(axis.text.y=element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.y = element_blank())+
  coord_cartesian(ylim = c(0, 1))

p3<-ggplot()+
  geom_line(data=df, aes(x=x, y=y3))+
  theme(axis.text.y=element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.y = element_blank())+
  coord_cartesian(ylim = c(0, 1))

p<-grid.arrange(p1, p2, p3, ncol=3)
1 Answers

there are multiple ways.

ggarrange from the egg package can help here.

     library(ggplot2)
     library(egg)
     df<-data.frame(x=1:5, y1=runif(5), y2=runif(5), y3=runif(5))
     
     p1<-ggplot()+
       geom_line(data=df, aes(x=x, y=y1))+
       coord_cartesian(ylim = c(0, 1))
     
     p2<-ggplot()+
       geom_line(data=df, aes(x=x, y=y2))+
       theme(axis.text.y=element_blank(),
             axis.title.y = element_blank(),
             axis.ticks.y = element_blank())+
       coord_cartesian(ylim = c(0, 1))
     
     p3<-ggplot()+
       geom_line(data=df, aes(x=x, y=y3))+
       theme(axis.text.y=element_blank(),
             axis.title.y = element_blank(),
             axis.ticks.y = element_blank())+
       coord_cartesian(ylim = c(0, 1))
     
     p<-grid.arrange(p1, p2, p3, ncol=3)
     
     p2<-ggarrange(p1, p2,p3, ncol=3)  

enter image description here

Related