我正在使用以下代码在惊人的包ggplot2中创建三组图:
w<-rnorm(100)
x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)
g<-rep(factor(LETTERS[1:4]), 25)
d<-data.frame(g,w,x,y,z)
library(ggplot2)
pw<-ggplot(d, aes(w, y))
px<-ggplot(d, aes(x, y))
pz<-ggplot(d, aes(z, y))
pw+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')
px+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')
pz+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')
Run Code Online (Sandbox Code Playgroud)
我会制作一个PDF文件,将这三组图中的每一组打印在同一页上.我的理解是,split.screen(c(3,1))
并且par(mfrow=c(3,1))
不能使用ggplot2图形,但是grid.layout()
从网格包中可以使用,所以我试过:
pdf(file="test.pdf")
pushViewport(viewport(layout=grid.layout(3,1)))
print(pw+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
print(px+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
print(pz+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
dev.off()
Run Code Online (Sandbox Code Playgroud)
但这最终是一个四页的PDF文件,第一页是空白的,每组数字跟随每页一个,x轴标签在底部向下.有没有办法在同一页面上制作包含所有图形集的PDF文件(并且没有空白页面引导!)?
Jos*_*ien 20
grid.arrange()
从gridExtra
包中你可能会有更好的时间使用:
p1 <- pw + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() +
stat_smooth(method='lm')
p2 <- px + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() +
stat_smooth(method='lm')
p3 <- pz + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() +
stat_smooth(method='lm')
grid.arrange(p1, p2, p3, ncol=1)
Run Code Online (Sandbox Code Playgroud)