由'plot'和'ggplot'并排生成的图

时间:2022-04-24 06:07:16

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?

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

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.

使用par或multiplot函数很容易将由同一函数创建的绘图放入一个页面,但我无法弄清楚上述问题。

2 个解决方案

#1


26  

You can do this using the gridBase package and viewPorts.

您可以使用gridBase包和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)

由'plot'和'ggplot'并排生成的图

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

此示例是Dylan Beaudette对此博客文章的修改版本

#2


2  

Yes. They are both grid-based graphics systems and return graphical objects. Take a look at the grid.arrange function in gridExtra package

是。它们都是基于网格的图形系统和返回图形对象。看一下gridExtra包中的grid.arrange函数

#1


26  

You can do this using the gridBase package and viewPorts.

您可以使用gridBase包和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)

由'plot'和'ggplot'并排生成的图

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

此示例是Dylan Beaudette对此博客文章的修改版本

#2


2  

Yes. They are both grid-based graphics systems and return graphical objects. Take a look at the grid.arrange function in gridExtra package

是。它们都是基于网格的图形系统和返回图形对象。看一下gridExtra包中的grid.arrange函数