问题描述
尊敬的Stackoverflow R专家
Dear Stackoverflow R Experts,
我正在尝试以简单的3x3布局创建具有9个散点图的页面.
I am attempting to create a page with 9 scatterplots in a simple 3x3 layout.
图1:8由plot()创建,第9个图由ggplot()创建.
Plots 1:8 are created by plot() and the 9th plot is created by ggplot().
按照页面布局正确设置地块1:8的大小和位置,但是第9个地块拒绝位于第9个位置,并且是完整尺寸的单独地块.
Plots 1:8 are sized and positioned correctly for the page layout, however the 9th plot refuses to sit in the 9th location and is a full sized, separate plot.
如何使第9个图块与图块1:8放置在正确的位置,且尺寸正确?
How do I make the 9th plot sit in the correct location, properly sized, on the same page as plots 1:8?
下面的代码是问题的简化示例.
The code (below) is a simplified example of the problem.
任何建议都将不胜感激!谢谢
Any advice is greatly appreciated!Thank you
par(mfrow=c(3,3))
df<-data.frame(c(0,0))
plot(df)
plot(df)
plot(df)
plot(df)
plot(df)
plot(df)
plot(df)
plot(df)
ggplot(df)+geom_point(aes(x="",y=""))
推荐答案
组合基本R绘图和ggplots可能不是最简单的任务,但这就是使用 gridGraphics
和的样子gridExtra
:
Combining base R plots and ggplots might not be the simplest task, but this is what it looks like using gridGraphics
and gridExtra
:
library(gridGraphics)
library(grid)
library(gridExtra)
library(ggplot2)
#save base-R plot
#taken from https://stackoverflow.com/questions/29583849/r-saving-a-plot-in-an-object
df<-data.frame(c(0,0))
plot(df)
#p <- recordPlot()
#plot.new()
#p
grid.echo()
a <- grid.grab()
a <- editGrob(a, vp=viewport(width=unit(2,"in")), gp=gpar(fontsize=10))
#save ggplot
b <- ggplot(df)+geom_point(aes(x="",y=""))
#plot all together
grid.arrange(a, a, a, a, a, a, a, a, b, nrow = 3, ncol = 3)
这篇关于每页R多个图混合图和ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!