0

I would like to make a composite graphic which is 3 panels wide by 8 panels tall. Inside each of the panel would be a graphic that takes multiple R commands to create, like the 1st 3 graphics might be

#1
plot(c(2:20), c(2:20), ylab="", xlab="", axes=FALSE)
par(new=TRUE)
plot(c(1:18, c(1:18), xact="n"), type="l", xlab="", ylab="")

#2
plot(c(2:20), c(2:20), ylab="", xlab="", axes=FALSE)
par(new=TRUE)
plot(c(1:18, c(1:18), xact="n"), type="l", xlab="", ylab="", col=2)

#3
plot(c(2:20), c(2:20), ylab="", xlab="", axes=FALSE, col=3)
par(new=TRUE)
plot(c(1:18, c(1:18), xact="n"), type="l", xlab="", ylab="")

and then I want to use a command like this to display those 3 side by side: grid.arrange(plot1, plot2, plot3)

I don't understand how to save #1, #2, and #3 each to be plot1, plot2 and plot3. Examples of how to save plots all seem to be based on a single plot command, not multiple commands to make the plot. The help page says grob but doesn't explain what an acceptable grob might be, or how to make it. Should I screenshot each of those and save it as a jpeg file?

thanks for any help.

2 Answers2

0

You can set par(mfrow = c(8, 3)). This will draw each plot in a single panel of an 8 x 3 grid. And will only move on to draw the next plot when plot.new is (implicitly) called.

par(mfrow = c(8, 3))
par(mar = c(0.5, 0.5, 0.5, 0.5)) # Otherwise margins are too large for 24 plots

for(i in 1:24) {
  plot(c(2:20), c(2:20), ylab = "", xlab = "", axes = FALSE, col = 3)
  par(new = TRUE)
  plot(c(1:18), c(1:18), xaxt = "n", type = "l", xlab = "", ylab = "")
}

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • thank you, that's getting close. Thing is I'm trying to add features to an existing plot, so I don't want to move on to the next plot with par(new=TRUE) because I'm using that to plot 2 time series together on same plot but different vertical axes. I know TWOORD is suppose to do this but I couldn't get it to work so gave up. – Julii Brainard Jan 31 '23 at 07:51
  • Also, I was just planning to have the graphic as a pdf run over at least 2 pages; the solution may just be one plot per page, it's only going to go into supplemental materials anyway. Hmmm. Cheers anyway. – Julii Brainard Jan 31 '23 at 07:52
  • So... i found out that grob = graphical objects, but that doesn't really help. And I found out that this code works well for saving multi-command plots: plot(c(2:20), c(2:20), ylab="", xlab="", axes=FALSE) par(new=TRUE) plot(c(1:18, c(1:18), xact="n"), type="l", xlab="", ylab="") plot1=recordPlot() but I'm still failing to get grid.arrange() to accept that, grid.arrange() gives me error msg = "only 'grobs' allowed in "gList" .. without any info how to make an eligible grob even though I have an R object that seems like a grob. The quest continues. – Julii Brainard Jan 31 '23 at 09:10
  • correction, grobs are only generated by ggplot, can't make them from plot.... https://stackoverflow.com/questions/42742815/only-grobs-allowed-in-glist – Julii Brainard Jan 31 '23 at 09:19
  • @JuliiBrainard grobs are produced by the grid graphics system (which is what ggplot is built upon). The base R graphics system (as in your plot examples) is quite different. My answer here shows how you arrange multiple base R plots on a single page. There are many options for arranging grobs and ggplots, but far less for base R graphics. – Allan Cameron Jan 31 '23 at 12:39
0

Update & solution:

Don't use grid.arrange(), don't use jpeg(), recordplot(), pdf() or ggplot...

The below code worked by adding features to the original plots. Then it helps (within RStudio) exporting results as high resolution PNG. I can label the row/columns separately well enough in MS Word.

par(mfrow=c(1,3))
#1
plot1=plot(c(2:20), c(2:20), ylab="", xlab="", axes=FALSE)
par(new=TRUE)
plot1= plot1 + plot(c(1:18, c(1:18), xact="n"), type="l", xlab="", ylab="")

#2
plot2 = plot(c(2:20), c(2:20), ylab="", xlab="", axes=FALSE)
par(new=TRUE)
plot2 = plot2 + plot(c(1:18, c(1:18), xact="n"), type="l", xlab="", ylab="", col=2)

#3
plot3 = plot(c(2:20), c(2:20), ylab="", xlab="", axes=FALSE, col=3)
par(new=TRUE)
plot3 = plot3 + plot(c(1:18, c(1:18), xact="n"), type="l", xlab="", ylab="")
Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30