ggplot中的grid.layout [英] grid.layout in ggplot
问题描述
我使用下面的代码在惊人的包ggplot2中创建三组图:
w <-rnorm (100)
x <-rnorm(100)
y <-rnorm(100)
z <-rnorm(100)
g <-rep(factor(LETTERS [1 (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')
我会制作一个PDF文件,打印在同一页上的图表。我的理解是 split.screen(c(3,1))
和 par(mfrow = c(3,1))
不会与ggplot2图形一起工作,但是grid包中的 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 ,scale ='fixed')+ coord_equal()+ stat_smooth(method ='lm'))
print(pz + geom_point()+ facet_grid(。〜g,scales ='fixed')+ coord_equal()+ stat_smooth(method ='lm'))
dev.off()
这是一个四页的PDF文件,第一页是空白的,每一组数字在每页之后,x轴标签在最下面。有没有办法在同一页面上制作一个包含所有图形的PDF文件(并且没有空白页面导致!)? 使用> grid.arrange()$ c $>可能会有更好的效果
c>,从 gridExtra
包:
p1 < - pw + geom_point()+ facet_grid(。〜g,scales ='fixed')+ coord_equal()+
stat_smooth(method ='lm')
p2 < - px + geom_point()+ facet_grid( ()方法='fixed')+ coord_equal()+
stat_smooth(method ='lm')
p3 < - pz + geom_point()+ facet_grid(。〜g,scales ='固定')+ coord_equal()+
stat_smooth(method ='lm')
grid.arrange(p1,p2,p3,ncol = 1)
I'm using the following code to create three sets of plots in the amazing package 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')
I would make a PDF file that has each of these three sets of plots printed on the same page. My understanding is thatsplit.screen(c(3,1))
andpar(mfrow=c(3,1))
won't work with ggplot2 graphics, but thatgrid.layout()
from the grid package would work so I tried:
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()
but this ends up being a four page PDF file with the first page being blank and each set of figures following one per page and the x-axis label way down at the bottom. Is there a way to make a PDF file with all the sets of figures on the same page (and without a blank page leading!)?
You'll probably have a better time using grid.arrange()
, from the gridExtra
package:
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)
这篇关于ggplot中的grid.layout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!