排列多个图并保持 x-axis 标签与轴齐平
Arrange multiple plots and keep the x-axis label flush with with axis
我正在使用 patchwork
包安排多个情节。其中一个图的文本是垂直排列的,这会将 x-axis 标签向下推(应该如此),但是当我与第二个图结合时,两个图中的 x-axis 标签都会向下移动。我想将第二个图的 x-axis 标签保留在其原始位置。用一个例子更容易解释:
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
但我想要:
这可能吗?我不受 patchwork
的束缚,我尝试了 gridExtra::grid.arrange()
但它调整了绘图的大小。
使用library(cowplot)
,p2
的x轴会下降,不像你的例子,但我希望它能帮助你。如果您需要让 x 轴的位置在同一位置,请告诉我。
@phalteman 的补充说明
在plot_grid
中加入, align = "h", axis = "b"
,果然如你所愿!!
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
我正在使用 patchwork
包安排多个情节。其中一个图的文本是垂直排列的,这会将 x-axis 标签向下推(应该如此),但是当我与第二个图结合时,两个图中的 x-axis 标签都会向下移动。我想将第二个图的 x-axis 标签保留在其原始位置。用一个例子更容易解释:
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
但我想要:
这可能吗?我不受 patchwork
的束缚,我尝试了 gridExtra::grid.arrange()
但它调整了绘图的大小。
使用library(cowplot)
,p2
的x轴会下降,不像你的例子,但我希望它能帮助你。如果您需要让 x 轴的位置在同一位置,请告诉我。
@phalteman 的补充说明
在plot_grid
中加入, align = "h", axis = "b"
,果然如你所愿!!
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