我试图使用grid.arrange来在同一页上显示由ggplot生成的多个图表。这些图表使用相同的x数据,但具有不同的y变量。由于y数据具有不同的比例尺,这些图表的大小不同。
我尝试了在ggplot2中使用各种主题选项来更改图表大小并移动y轴标签,但都无法使图表对齐。我希望将这些图表排列成2 x 2的正方形,以使每个图表具有相同的大小,并使x轴对齐。
以下是一些测试数据:
A <- c(1,5,6,7,9)
B <- c(10,56,64,86,98)
C <- c(2001,3333,5678,4345,5345)
D <- c(13446,20336,24333,34345,42345)
L <- c(20,34,45,55,67)
M <- data.frame(L, A, B, C, D)
我正在使用的用于绘图的代码:
x1 <- ggplot(M, aes(L, A,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x2 <- ggplot(M, aes(L, B,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x3 <- ggplot(M, aes(L, C,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x4 <- ggplot(M, aes(L, D,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
grid.arrange(x1,x2,x3,x4,nrow=2)
如果您运行此代码,您会发现底部两个图的绘图区较小,这是由于y轴单位长度更长造成的。如何使实际绘图窗口相同?