Reputation: 545
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 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
Reputation: 6769
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
)
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