有没有办法将R中plot
函数生成的图和R中ggplot
函数生成的图并排放在一页中?
使用 par
或 multiplot
函数很容易将同一函数创建的绘图放入一页,但我无法弄清楚上述问题。
最佳答案
您可以使用 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)
此示例是 this blog post 的修改版本通过 Dylan Beaudette
关于r - 由 'plot' 和 'ggplot' 并排生成的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021863/