使用grid.arrange保留图的比例 [英] Preserve proportion of graphs using grid.arrange
问题描述
我尝试使用 grid.arrange
来安排多个图。
这本书完成了工作,并在调用时:
p1 < - ggplot(subset(mtcars, aes(wt,mpg,color = cyl))+ geom_point()
p2 <-ggplot(subset(mtcars,cyl = 8),aes(wt,mpg,color = cyl)) + geom_point()
grid.arrange(p1,p2,ncol = 2)
我收到了两张漂亮的小图,大小对称:
我的图表指的是不同的参数,但它们的组合使用相同的颜色编码。所以我想删除除了一个以外的所有图例并找到一个好地方。
然而,当我尝试时:
$ (颜色=颜色=圆柱体))+ geom_point()+导轨(颜色=圆柱体) FALSE)
grid.arrange(p3,p2,ncol = 2)
没有图例的情节得到(正确)更大:
我想保持大小(作为x轴的长度)在图中保持不变。 b
$ b
我知道我可以在这里使用faceting,但是我还需要结合使用facets很难实现的各种图形。
可以用 grid.arrange
来完成吗?任何其他解决方案,可以帮助这里?
不像@Josh的解决方案那么简单,但你可以做到这一点使用grid.arrange可以保留或指定图的纵横比,但是您需要为图例制作 tableGrob
。我回答了一个类似这里的问题,这是我从ggplot2图例中获得tableGrob的方便代码:
##制作您的传奇的图表
tmp< - ggplot_gtable(ggplot_build(p2))
leg< ; - 其中(sapply(tmp $ grobs,function(x)x $ name)==guide-box)
legend< - tmp $ grobs [[leg]]
#绘制使用宽度和高度的对象,并重新定义宽高比值
#我们制作一个3列的网格布局,分别用于图表和一个图例
grid.newpage()
pushViewport(viewport(layout = grid.layout(1,3,widths = unit(c(0.4,0.4,0.2),npc),heights = unit(c(0.45,0.45,0.45),npc尊重=矩阵(rep(1,3),1))))
print(p1 + theme(legend.position =none),vp = viewport(layout.pos.row = 1,layout.pos .col = 1))
print(p2 + theme(legend.position =none),vp = viewport(layout.pos.row = 1,layout.pos.col = 2))
upViewport(0)
vp3< - viewport(width = unit(0.2,npc),x = 0.9,y = 0.5)
pushViewport(vp3)
grid。 draw(legend)
popViewport()
I'm trying to arrange multiple plots using grid.arrange
.
It does the job by the book, and when calling:
p1 <- ggplot(subset(mtcars, cyl = 4), aes(wt, mpg, colour = cyl)) + geom_point()
p2 <- ggplot(subset(mtcars, cyl = 8), aes(wt, mpg, colour = cyl)) + geom_point()
grid.arrange(p1, p2, ncol = 2)
I get two nice plots, symmetrical in size:
My graphs refer to different parameters but they do share the same colour coding for groups. So I'd like to remove the legend from all but one and find a nice place for it.
However when I try:
p3 <- ggplot(subset(mtcars, cyl = 8), aes(wt, mpg, colour = cyl)) + geom_point() + guides(colour=FALSE)
grid.arrange(p3, p2, ncol = 2)
The plot without the legend gets (correctly) bigger:
I'd like to keep the size (as a length of x axis) to stay the same across graphs.
I'm aware I could use faceting here, but I'll also need to combine various graphs that (I think) will be hard to implement using facets..
Is it possible to do it with grid.arrange
? Any other solutions that could help here?
Not nearly as elegantly simple as @Josh 's solution, but you can do this with grid.arrange which allows you to preserve or specify the aspect ratio of the plots, but you need to make a tableGrob
for your legend. I answered a simillar question here which is where I got the handy code for making a tableGrob from a ggplot2 legend:
## Make a tableGrob of your legend
tmp <- ggplot_gtable(ggplot_build(p2))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
# Plot objects using widths and height and respect to fix aspect ratios
# We make a grid layout with 3 columns, one each for the plots and one for the legend
grid.newpage()
pushViewport( viewport( layout = grid.layout( 1 , 3 , widths = unit( c( 0.4 , 0.4 , 0.2 ) , "npc" ) ,heights = unit( c( 0.45 , 0.45 , 0.45 ) , "npc" ) , respect = matrix(rep(1,3),1) ) ) )
print( p1 + theme(legend.position="none") , vp = viewport( layout.pos.row = 1 , layout.pos.col = 1 ) )
print( p2 + theme(legend.position="none") , vp = viewport( layout.pos.row = 1, layout.pos.col = 2 ) )
upViewport(0)
vp3 <- viewport( width = unit(0.2,"npc") , x = 0.9 , y = 0.5)
pushViewport(vp3)
grid.draw( legend )
popViewport()
这篇关于使用grid.arrange保留图的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!