I am arranging multiple plot using the patchwork
package. One of the plots has the text arranged vertically, which pushes the x-axis label down (as it should), but when I combine with a second plot, the x-axis labels in both plots are moved down. I would like to keep the x-axis label of the second plot in its original position. Easier to explain with an example:
library(ggplot2)
library(patchwork)
# Toy data
mtcars2 <- mtcars[1:5, ]
mtcars2$mod <- row.names(mtcars2)
# make 2 plots
p1 <- ggplot(mtcars2, aes(mod, mpg))
geom_col()
theme(axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1))
p2 <- ggplot(mtcars2, aes(hp, disp))
geom_point()
# arrange plots next to each other
p1 p2
But I want:
Is this possible? I'm not tied to patchwork
and I tried gridExtra::grid.arrange()
but that resized the plots instead.
CodePudding user response:
Using library(cowplot)
, x axis of p2
will go down, not like your example, but i wish it help you. If you need to let location of x - axis in same position, please let me know.
Additional note from @phalteman
By adding , align = "h", axis = "b"
in plot_grid
, it really becomes what you wanted!!
library(cowplot)
mtcars2 <- mtcars[1:5, ]
mtcars2$mod <- row.names(mtcars2)
# make 2 plots
p1 <- ggplot(mtcars2, aes(mod, mpg))
geom_col()
theme(axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1))
p2 <- ggplot(mtcars2, aes(hp, disp))
geom_point()
# arrange plots next to each other
plot_grid(p1, p2, align = "h", axis = "b") #Thanks to @phalteman