首页 > 解决方案 > 在ggplot2中对齐轴并使用多图轴标签

问题描述

我正在尝试在 ggplot2 中添加多图轴标签和对齐图轴。

这是 mtcars 数据的示例。

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
)

我希望轴与红色箭头对齐。

我想保留左下轴标签并对齐轴。“底部”标签将略低于绘图区域。

标签: rggplot2

解决方案


编辑

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)

reprex 包于 2021-04-06 创建 (v2.0.0 )


推荐阅读