我正在尝试使用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)
Run Code Online (Sandbox Code Playgroud)
我用来绘制的代码:
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)
Run Code Online (Sandbox Code Playgroud)
如果运行此代码,您将看到由于y轴单位的长度较大,底部两个图的绘图区域较小.
如何使实际绘图窗口相同?
San*_*att 21
编辑
更简单的解决方案是:1)使用cowplot
包(见这里的答案); 或2)使用egg
github上提供的包.
# devtools::install_github("baptiste/egg")
library(egg)
library(grid)
g = ggarrange(x1, x2, x3, x4, ncol = 2)
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
原版的
次要编辑:更新代码.
如果你想保留轴标签,然后使用一些摆弄,并从这里借用代码,这就完成了工作.
library(ggplot2)
library(gtable)
library(grid)
library(gridExtra)
# Get the widths
gA <- ggplotGrob(x1)
gB <- ggplotGrob(x2)
gC <- ggplotGrob(x3)
gD <- ggplotGrob(x4)
maxWidth = unit.pmax(gA$widths[2:3], gB$widths[2:3],
gC$widths[2:3], gD$widths[2:3])
# Set the widths
gA$widths[2:3] <- maxWidth
gB$widths[2:3] <- maxWidth
gC$widths[2:3] <- maxWidth
gD$widths[2:3] <- maxWidth
# Arrange the four charts
grid.arrange(gA, gB, gC, gD, nrow=2)
Run Code Online (Sandbox Code Playgroud)
替代解决方案:
有rbind
和cbind
功能在gtable
包grobs组合成一个GROB.对于此处的图表,应使用设置宽度size = "max"
,但CRAN版本会gtable
引发错误.
一种选择是检查grid.arrange
图,然后使用size = "first"
或size ="last"`选项:
# Get the ggplot grobs
gA <- ggplotGrob(x1)
gB <- ggplotGrob(x2)
gC <- ggplotGrob(x3)
gD <- ggplotGrob(x4)
# Arrange the four charts
grid.arrange(gA, gB, gC, gD, nrow=2)
# Combine the plots
g = cbind(rbind(gA, gC, size = "last"), rbind(gB, gD, size = "last"), size = "first")
# draw it
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
第二种选择是bind
从gridExtra
包中获取功能.
# Get the ggplot grobs
gA <- ggplotGrob(x1)
gB <- ggplotGrob(x2)
gC <- ggplotGrob(x3)
gD <- ggplotGrob(x4)
# Combine the plots
g = cbind.gtable(rbind.gtable(gA, gC, size = "max"), rbind.gtable(gB, gD, size = "max"), size = "max")
# Draw it
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
Cla*_*lke 10
这正是我编写cowplot包的那种问题.它可以在该包中的一行中完成:
require(cowplot) # loads ggplot2 as dependency
# re-create the four plots
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')
# arrange into grid and align
plot_grid(x1, x2, x3, x4, align='vh')
Run Code Online (Sandbox Code Playgroud)
这是结果:
(请注意,cowplot会更改默认的ggplot2主题.如果你真的想要,你可以回到灰色的那个.)
作为额外功能,您还可以在每个图表的左上角添加绘图标签:
plot_grid(x1, x2, x3, x4, align='vh', labels=c('A', 'B', 'C', 'D'))
Run Code Online (Sandbox Code Playgroud)
结果:
我labels
几乎在每个多部分图表上使用该选项.
我会使用faceting来解决这个问题:
library(reshape2)
dat <- melt(M,"L") # When in doubt, melt!
ggplot(dat, aes(L,value)) +
geom_point() +
stat_smooth(method="lm") +
facet_wrap(~variable,ncol=2,scales="free")
Run Code Online (Sandbox Code Playgroud)
注意:外行人可能会错过刻面之间的比例不同.