【问题标题】:Arrange multiple plots and keep the x-axis label flush with with axis排列多个绘图并保持 x 轴标签与轴齐平
【发布时间】:2021-11-08 11:04:18
【问题描述】:

我正在使用patchwork 包安排多个情节。其中一个图的文本垂直排列,这将 x 轴标签向下推(应该如此),但是当我与第二个图结合时,两个图中的 x 轴标签都向下移动。我想将第二个图的 x 轴标签保持在其原始位置。举个例子更容易解释:

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(),但它调整了地块的大小。

【问题讨论】:

    标签: r ggplot2 patchwork


    【解决方案1】:

    使用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 
    

    【讨论】:

    • 在您对plot_grid() 的调用中包含align = "h", axis = "b",它完全符合OP 的要求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多