Save plot with a given aspect ratio Save plot with a given aspect ratio r r

Save plot with a given aspect ratio


You can use grid functions to calculate the full size of the ggplot grob, but there are (edit: at least) two caveats:

  • an extra device window will open, to do the unit conversion

  • the plot panel size will be 0 by default, as it is meant to be calculated on-the-fly according to the device (viewport) it lives in, not the opposite.

That being said, the following function attempts to open a device that fits the ggplot exactly,

library(ggplot2)library(grid)sizeit <- function(p, panel.size = 2, default.ar=1){  gb <- ggplot_build(p)  # first check if theme sets an aspect ratio  ar <- gb$plot$coordinates$ratio  # second possibility: aspect ratio is set by the coordinates, which results in   # the use of 'null' units for the gtable layout. let's find out  g <- ggplot_gtable(gb)  nullw <- sapply(g$widths, attr, "unit")  nullh <- sapply(g$heights, attr, "unit")  # ugly hack to extract the aspect ratio from these weird units  if(any(nullw == "null"))    ar <- unlist(g$widths[nullw == "null"]) / unlist(g$heights[nullh == "null"])  if(is.null(ar)) # if the aspect ratio wasn't specified by the plot       ar <- default.ar  # ensure that panel.size is always the larger dimension  if(ar <= 1 ) panel.size <- panel.size / ar  g$fullwidth <- convertWidth(sum(g$widths), "in", valueOnly=TRUE) +     panel.size  g$fullheight <- convertHeight(sum(g$heights), "in", valueOnly=TRUE) +     panel.size / ar  class(g) <- c("sizedgrob", class(g))  g}print.sizedgrob <- function(x){  # note: dev.new doesn't seem to respect those parameters  # when called from Rstudio; in this case it   # may be replaced by x11 or quartz or ...  dev.new(width=x$fullwidth, height=x$fullheight)  grid.draw(x)}p1 <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + coord_fixed() +  theme(plot.background = element_rect(colour = "red"))p2 <- p1 + aes(x = mpg, y = wt)# need for an explicit dummy device open, otherwise it's a bit off# for no apparent reason that I can understanddev.new() sizeit(p1, 0.1)

enter image description here

sizeit(p2, 2)

enter image description here


Based on baptiste's answer I stripped his code down to return the aspect ratio as suggested by geotheory. This was much more convenient for me, because I either wanted a fixed width or height and also passed everything through an existing wrapper function that also adds fonts to my pdf.

Oh, and if you used facets you need to take them into account manually. Divide by rows and multiply by columns. Not sure whether there is a better way.....

ggGetAr <- function(p, default.ar=-1){    gb <- ggplot_build(p)    # first check if theme sets an aspect ratio    ar <- gb$plot$coordinates$ratio    # second possibility: aspect ratio is set by the coordinates, which results in     # the use of 'null' units for the gtable layout. let's find out    g <- ggplot_gtable(gb)    nullw <- sapply(g$widths, attr, "unit")    nullh <- sapply(g$heights, attr, "unit")    # ugly hack to extract the aspect ratio from these weird units    if(any(nullw == "null"))        ar <- unlist(g$widths[nullw == "null"]) / unlist(g$heights[nullh == "null"])    if(is.null(ar)) # if the aspect ratio wasn't specified by the plot        ar <- default.ar    ar[1]}


If you use ggsave you can simply specify the width and height of the graphics device. If you specify the aspect ratio of the plot itself, it is also good to have this aspect ratio (roughly) in your graphics device. The unit of height and width when saving pdf is inches:

ggplot(...) # make a plot hereggsave("plot.pdf", width = 10)

Now you only have to transform the 10 cm into inches. In addition, height is not forced to a certain aspect ratio if you do not specify it. If you want a 16:9 aspect ratio, you can easily calculate the height based on the width:

ggplot(...) # make plotwidth = 10height = (9/16) * widthggsave("plot.pdf", width = width, height = height)

You could wrap this in a function if you really want to.


edit: The crux is to synchronize the aspect ratio of the plot (through coord_fixed()) and the aspect ratio of the graphics device. For example

library(ggplot2)ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + coord_fixed()ggsave("plt.png", width = 7, height = 7)

enter image description hereleads to a lot of white space. While the following ggsave call, which has a much better fit in aspect ratio, does not have this amount of white space (sorry for the large picture, could not set the maximum size :)):

ggsave("plt.png", width = 2, height = 7)

enter image description here