'plot' 和 'ggplot' 并排生成的图 [英] plots generated by 'plot' and 'ggplot' side-by-side

查看:42
本文介绍了'plot' 和 'ggplot' 并排生成的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 R 中 plot 函数生成的图和 ggplot 函数生成的图并排放置在一页中?

Is there a way to put the plot generated by plot function and the plot by ggplot function in R in one page side-by-side?

使用 parmultiplot 函数可以很容易地将同一函数创建的绘图放在一个页面中,但我无法弄清楚上面的问题.

It is easy to put plots created by the same function into one page using par or multiplot function, but I can't figure out the above question.

推荐答案

您可以使用 gridBase 包和 viewPorts 来做到这一点.

You can do this using the gridBase package and viewPorts.

library(grid)
library(gridBase)
library(ggplot2)

# start new page
plot.new() 

# setup layout
gl <- grid.layout(nrow=1, ncol=2)
# grid.show.layout(gl)

# setup viewports
vp.1 <- viewport(layout.pos.col=1, layout.pos.row=1) 
vp.2 <- viewport(layout.pos.col=2, layout.pos.row=1) 
# init layout
pushViewport(viewport(layout=gl))
# access the first position
pushViewport(vp.1)

# start new base graphics in first viewport
par(new=TRUE, fig=gridFIG())

plot(x = 1:10, y = 10:1)

# done with the first viewport
popViewport()

# move to the next viewport
pushViewport(vp.2)

ggplotted <- qplot(x=1:10,y=10:1, 'point')
# print our ggplot graphics here
print(ggplotted, newpage = FALSE)

# done with this viewport
popViewport(1)

这个例子是这篇博文的修改版,作者是迪伦博德特

This example is a modified version of this blog post by Dylan Beaudette

这篇关于'plot' 和 'ggplot' 并排生成的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆