rggplot2size

ggplot2: fix absolute size of ggplot on onscreen device (RStudio plots pane)


This feels like the most basic question that must have been answered already, but I have Googled for hours and not found a useable answer.

I will be generating a huge number of 3 inch by 3 inch plots that will all be saved using ggsave(..., width = 3, height = 3, units = "in"). I would like to make sure they always display on screen (in the RStudio plots pane) as 3 inches by 3 inches, so that I can quickly check that they look right before I save them.

But I just can't seem to find a way to do this! Things I have tried:

  1. egg::set_panel_size is almost it!! It is great until I, say, add a title to my plot, and then it is only the panel size that is fixed (so the whole plot is then bigger than 3x3). But the fact that I can fix the sizes of the panels makes me think it must also be possible to do the same thing with the whole plot? And that is exactly what I need. (Though even better would be to render the plot as if it were 3in x 3in regardless of the size of the screen, but this would do!)

  2. dev.new is possible but a bit clunky in that I have to have a separate window open, which makes iterative refinement of my graphics rather slow and painstaking (and also not robust to, say, a silly moment where I accidentally resize it).

  3. cowplot::draw_plot allows me to set the height of the plot relative to the window size but not (as far as I can see?) an absolute size.

I've tried a lot of other things (e.g. trying to hack the ggplotGrob object) but I can't seem to see a way to do this. To be clear, this is not about saving at the specified size (that bit is easy) it is about generating a rapid preview that will look the same as my ggsaved plot. Thank you!

dev.new


Solution

  • I don't think you can programmatically resize the plotting window, but in any case a better solution would be to have the plot drawn in the centre of the plotting window at a fixed size. You can do this by drawing the ggplot to a fixed-size viewport within the plotting window. This will remain the same size and aspect ratio however your IDE is set up.

    The following function will allow a ggplot object to be drawn in this format, with dimensions of your choosing:

    draw <- function(plot, x_in = 3, y_in = 3) {
      
      grid::grid.newpage()
      
      grid::rectGrob(gp = grid::gpar(fill = "gray")) |>
      grid::grid.draw()
      
      grid::viewport(width  = ggplot2::unit(x_in, "in"), 
                     height = ggplot2::unit(y_in, "in")) |>
        grid::pushViewport()
      
      ggplot2::ggplot_build(plot) |>
        ggplot2::ggplot_gtable() |>
        grid::grid.draw()
    }
    

    For example:

    library(ggplot2)
    
    p <- ggplot(iris, aes(Sepal.Width, Petal.Length, color = Species)) +
      geom_point()
    
    draw(p)
    

    If you don't want to have to call draw(p) every time, but just have ggplots automatically draw this way during an interactive session, you can do

    print.ggplot <- draw
    

    after defining the above function.

    This results in the following behaviour

    ggplot(mtcars, aes(wt, mpg, color = factor(gear))) +
      geom_point()
    

    Created on 2023-09-27 with reprex v2.0.2