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
The trick is to put the plots in a list.
myPlots = list(p1, p10)
ggpubr::ggarrange(plotlist = myPlots, nrow = 1)
Warning messages:
1: PackagegridGraphicsis required to handle base-R plots. Substituting empty plot.
2: PackagegridGraphicsis 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With