1

Notice this is different from other questions because I use coord_fixed() which changes the spacing.

Here's my minimal working example:

WITH COORD FIXED

library(ggplot2)
library(gridExtra)
x = runif(100)
p <- ggplot(data.frame(x=x, y=x)) + geom_line(aes(x, y)) + theme_minimal() + coord_fixed()
grid.arrange(p, p, p, p, ncol=2)

enter image description here

WITHOUT COORD FIXED

library(ggplot2)
library(gridExtra)
x = runif(100)
p <- ggplot(data.frame(x=x, y=x)) + geom_line(aes(x, y)) + theme_minimal()
grid.arrange(p, p, p, p, ncol=2)

enter image description here

QUESTION How can I make the first plot look like the second, just with fixed coordinates?

tjebo
  • 21,977
  • 7
  • 58
  • 94
Euler_Salter
  • 3,271
  • 8
  • 33
  • 74

2 Answers2

2

My recommendation is using patchwork.

library(patchwork)

p + p + p + p

The claim to fame of patchwork is mainly two things: A very intuitive API, and a layout engine that promises to keep your plots aligned no matter how complex a layout you concoct.

You can check this post about patchwork. The author is the main maintainer o ggplot2.

Johan Rosa
  • 2,797
  • 10
  • 18
2

I also like the patchwork package, but let's stick to gridExtra. I think you may have looked at the RStudio viewer? It gives sometimes quite weird results.

If you (what I assume you will do in the end) create a device (with or without specific dimensions), at least on my machine this effect does not happen.

dev.new(width = 5, height = 4, noRStudioGD = TRUE)
grid.arrange(p, p, p, p, ncol=2)
dev.off()

Screenshot enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • I had no idea there was a thing called device to be fair.. I'll look into it thank you! – Euler_Salter May 02 '20 at 21:43
  • 1
    @Euler_Salter check https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/Devices.html. A device is basically what you're drawing into, (and you need it to save figures). If you save as pdf, you create a pdf device first. This also happens with functions like ggsave, but less explicitely. – tjebo May 03 '20 at 11:19
  • 1
    As a rule of thumb, the Rstudio viewer pane is kind of lousy to show you what you actually get in the end. I guess that's because it can be resized in any dimensions and they kind of create a device "within" as a kind of viewport (I am not sure about that though). I control figure layouts in the device I want the figures in - usually pdf in my case. (and I always explicitly define the dimensions!) – tjebo May 03 '20 at 11:25
  • thank you so much!! I need to save images to go on a Latex document (for a paper possibly). Do you have any suggestion about which device to use? – Euler_Salter May 03 '20 at 13:20
  • @Euler_Salter check out https://stackoverflow.com/questions/1890215/getting-r-plots-into-latex. I guess it also depnds how you create your latex in the end. I am currently not yet using latex (but this will change), but am using rmarkdown (knitr) quite a lot - it can also produce pdf via latex - and you can get the tex files "for free" whilst knitting the document https://stackoverflow.com/questions/37701995/rmarkdown-retain-tex-file – tjebo May 03 '20 at 14:23