-2

I have created several plots that each required several lines of code.

I am trying to combine them using par(mfrow=c(1,6)). However, rather than programming the plot creation below this code (which would be rather messy), I would like simply 'list' the saved plots underneath that command, and have those combined.

I have tried

par(mfrow=c(1,5))

# The premade plots
scatter_1  
scatter_2
scatter_3
scatter_4
scatter_5

But the plots still are outputted individually rather than combined. Any suggestions?

Cassandra
  • 137
  • 1
  • 9
  • What types of plots are these? Are they `ggplot2` plots? `par()` only works for base graphics. Many packages like `ggplot2` use grid graphics rather than base graphics. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 15 '19 at 20:45
  • 1
    See the `patchwork` or `cowplot` or `egg` or `gridExtra` packages. – Jon Spring Oct 15 '19 at 21:06

1 Answers1

0

Here is a really quick example of how to achieve what you want using gridExtra::grid.arrange. The ncol argument controls the number of columns. Use nrow=2 to stack the plots on top of each other. I suggest that you go through this read.

library(ggplot2)
library(gridExtra)

p1 <- ggplot(data = mtcars) +
  geom_point(aes(hp, mpg))

p2 <- ggplot(data = mtcars) +
  geom_point(aes(wt, mpg))

grid.arrange(p1, p2, ncol = 2)
slava-kohut
  • 4,203
  • 1
  • 7
  • 24
  • That worked perfectly, thank you! Any direction on specifiying the width or height of the combined plot? I'm finding this function tends to squish the plot in undesirable ways (all plots equally). I've tried specifying ```fig.width``` and ```fig.height``` in my r markdown chunk naming but no luck. – Cassandra Oct 16 '19 at 06:28
  • @Cassandra if you liked the answer, consider upvoting or accepting. What you did should have worked, check your code again. – slava-kohut Oct 16 '19 at 13:12