Henry Holm
Henry Holm

Reputation: 545

Aligning axis and with multiplot axis labels in ggplot2

I am trying to both add multi-plot axis labels and align plots axis in ggplot2.

Here is an example with mtcars data.

library(ggplot2)
library(gridExtra)

#make some plots
c1 <- ggplot(mtcars,aes(mpg,cyl)) + geom_point() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())
c2 <- ggplot(mtcars,aes(mpg,cyl)) + geom_line() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())
c3 <- ggplot(mtcars,aes(mpg,cyl)) + geom_path() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())
c4 <- ggplot(mtcars,aes(mpg,cyl)) + geom_violin() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())

#arrange and add axis
grid.arrange(
  arrangeGrob(c1,c2,ncol = 1, left = "Left Axis",bottom = "Bottom Left"),
  arrangeGrob(c3,c4,ncol = 1, left = "Middle Axis"),
  ncol = 2
)

I want the axis aligned at the red arrows.

I want to keep the Bottom Left axis label and align the the axis as well. The 'bottom' label will be slightly below the plot area.

Upvotes: 2

Views: 180

Answers (1)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

Edit

c1 <- ggplot(mtcars,aes(mpg,cyl)) + geom_point() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())
c2 <- ggplot(mtcars,aes(mpg,cyl)) + geom_line() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())
c3 <- ggplot(mtcars,aes(mpg,cyl)) + geom_path() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())
c4 <- ggplot(mtcars,aes(mpg,cyl)) + geom_violin() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())

#arrange and add axis
grid.arrange(
  arrangeGrob(c1,c2,ncol = 1, left = "Left Axis",bottom = "Bottom Left"),
  arrangeGrob(c3,c4,ncol = 1, left = "Middle Axis", bottom = " "),
  ncol = 2
)

enter image description here

Or

library(ggplot2)
library(gridExtra)

c1 <- ggplot(mtcars,aes(mpg,cyl)) + geom_point() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())
c2 <- ggplot(mtcars,aes(mpg,cyl)) + geom_line() + theme(axis.title.y = element_blank()) + labs(x = "Bottom") 
c3 <- ggplot(mtcars,aes(mpg,cyl)) + geom_path() + theme(axis.title.x = element_blank(),axis.title.y = element_blank())
c4 <- ggplot(mtcars,aes(mpg,cyl)) + geom_violin() + theme(axis.title.y = element_blank()) + labs(x = " ")

library(ggpubr)
ggarrange(c1, c3, c2, c4)

Created on 2021-04-06 by the reprex package (v2.0.0)

Upvotes: 2

Related Questions