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

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

问题描述

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

使用 par multiplot 函数,但我无法弄清楚上述问题。 您可以使用 gridBase 包和viewPorts来完成此操作。

 <$ c $ b $ library(gridBase)
library(ggplot2)

#开始新页面
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)
#初始布局
pushViewport(viewport(layout = gl))
#访问第一个位置
pushViewport(vp.1)

#在fir中启动新的基础图形st视口
par(new = TRUE,fig = gridFIG())

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

#done与第一个视口
popViewport()

#移动到下一个视口
pushViewport(vp.2)

ggplotted< - qplot(x = 1:10,y = 10:1,'point')
#在这里打印我们的ggplot图形
print(ggplotted,newpage = FALSE)


popViewport(1)


这个例子是本博文 Dylan Beaudette


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?

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.

解决方案

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天全站免登陆