Reputation: 1089
I create 2 plots, p1 and p10 and record them as follows:
plot(data$Fwd_EY, data$SPNom1YrFwdRet, pch = 16, cex = 1.0, col = "blue")
p1 <- recordPlot()
dev.off()
plot(data$Fwd_EY, data$SPNom10YrFwdRet, pch = 16, cex = 1.0, col = "blue")
p10 <- recordPlot()
dev.off()
I print P1 and P10 to .png files, and would then like to view both plots side by side before printing them to a single .png file. I have tried variants of the following with no success.
myPlots = c(p1, p10)
ggarrange(plotlist = myPlots, nrow = 1)
par(mfrow=c(1,2))
p1
p10
nf <- layout( matrix(c(1,2), ncol=1) )
p1
p10
In some cases, R seems to require the plots to be ggplots. In other cases the plots simply print full screen. How can I achieve my goal?
Thanks in advance
Thomas Philips
Upvotes: 3
Views: 1971
Reputation: 76621
The trick is to put the plots in a list
.
myPlots = list(p1, p10)
ggpubr::ggarrange(plotlist = myPlots, nrow = 1)
Warning messages:
1: PackagegridGraphics
is required to handle base-R plots. Substituting empty plot.
2: PackagegridGraphics
is required to handle base-R plots. Substituting empty plot.
library(gridGraphics)
#Loading required package: grid
myPlots = list(p1, p10)
ggpubr::ggarrange(plotlist = myPlots, nrow = 1)
Data
plot(1:10, pch = 16, cex = 1.0, col = "blue")
p1 <- recordPlot()
dev.off()
plot(10:1, pch = 16, cex = 1.0, col = "red")
p10 <- recordPlot()
dev.off()
Upvotes: 2
Reputation: 12586
Attempting to pre-empt OP's response to my second comment above...
Here's a solution which starts with raw data and overlays a best fit linear regression on a scatterplot.
d <- tibble(
X=runif(40, 0, 100),
Y=-5 + 0.3 * X + rnorm(40)
)
d %>% ggplot(aes(x=X, y=Y)) +
geom_point() +
stat_smooth(method = "lm")
Upvotes: 0