Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot multiple plots that were NOT created using ggplot in a row / grid

Tags:

plot

r

ggplot2

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

like image 416
Thomas Philips Avatar asked May 21 '26 08:05

Thomas Philips


1 Answers

The trick is to put the plots in a list.

myPlots = list(p1, p10)
ggpubr::ggarrange(plotlist = myPlots, nrow = 1)

Warning messages:
1: Package gridGraphics is required to handle base-R plots. Substituting empty plot.
2: Package gridGraphics 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)

enter image description here

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()
like image 166
Rui Barradas Avatar answered May 23 '26 20:05

Rui Barradas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!