首页 > 解决方案 > R中混合多个图形的轴标题对齐

问题描述

我正在使用ggplot2包来制作我的图表。我有 2 个图表,我plot_grid()cowplot包中混合使用。

library(ggplot2)
library(cowplot)

x1 <- c(52.67, 46.80, 41.74, 40.45)
y1 <- c(1.726219351, 1.842421805, 1.790801758, 1.449997494)
x2 <- c(51.07, 65.97, 61.01, 49.25)
y2 <- c(5.39765063, 0.215293169, 0.694595893, 1.501089083)

DF <- data.frame(x1, y1, x2, y2)

p1 <- ggplot(DF, aes(x1, y1)) + 
  geom_point() +
  theme(aspect.ratio = 1)

p2 <- ggplot(DF, aes(x2, y2)) + 
  geom_point() +
  theme(aspect.ratio = 1)

plot_grid(p1, p2)

plot_grid(p1, p2, align = "hv")

第二个图(p2_ 但是,y 轴标签与原始绘图 ( ) 大小(图像中的蓝线)保持在同一位置。有没有办法让标签靠近 y 轴作为原始图?p1aligncowplotp2

在此处输入图像描述

标签: rggplot2alignmentaxis-labelscowplot

解决方案


具有固定轴比的图形对齐可能很困难。在任何情况下,如果你使用这个axis参数除了align事情之外。有关更多详细信息,请参见此处:https ://wilkelab.org/cowplot/articles/aligning_plots.html

library(ggplot2)
library(cowplot)
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************

x1 <- c(52.67, 46.80, 41.74, 40.45)
y1 <- c(1.726219351, 1.842421805, 1.790801758, 1.449997494)
x2 <- c(51.07, 65.97, 61.01, 49.25)
y2 <- c(5.39765063, 0.215293169, 0.694595893, 1.501089083)

DF <- data.frame(x1, y1, x2, y2)

p1 <- ggplot(DF, aes(x1, y1)) + 
  geom_point() +
  theme(aspect.ratio = 1)

p2 <- ggplot(DF, aes(x2, y2)) + 
  geom_point() +
  theme(aspect.ratio = 1)

plot_grid(p1, p2, align = "hv", axis = "tbrl")

reprex 包(v0.3.0)于 2019 年 7 月 26 日创建


推荐阅读