Ggsave aspect ratio / whitespace (use case: favicon for blogdown)

I thought it would be fun to use ggplot2 to make a favicon for my blogdown site, but I'm having trouble using ggsave() to get the right dimensions/resolutions. Specifically, how can I save my plot while maintaining its shape and avoiding additional whitespace?

Ideally, a favicon should be saved as a 16x16 or 32x32 pixel square. My thought on the steps to this was:

However, my resulting image still has whitespace at the bottom edge. I notice that I do not get whitespace if I don't specify height, width, or dpi in my call to ggsave(). However, in this case the resulting image is not saved as a square.

I know a hack solution is just to save the plot within the IDE's plot viewer, but I'd like to get this working right. If anyone has advice/experience from a similar situation, I'd appreciate it! A reprex is below.

~

As a concrete example, here is the code I am using to create my plot.

library(ggplot2)

ggplot(
  expand.grid(x = -100:100, y = -100:100),
  aes( x = x, y = y)) +
  
  geom_tile(aes(fill = abs(x) )) +
  scale_fill_continuous(high = "cornflowerblue", low = "lightskyblue2") +
  annotate(geom = "text", label = "ER", 
x = 0, y = 0, size = 30, fontface = 6, col = "white") +
  
  # cleaning up layout
  scale_x_continuous(expand=c(0,0)) +
  scale_y_continuous(expand=c(0,0)) +
  cowplot::theme_nothing() +
  theme(aspect.ratio=1) 

The output itself appears square with no whitespace.

Here is my call to ggsave():

ggsave(filename = "./static/favicon.png",
       height = 2, width = 2, dpi = 16, plot = last_plot(), device='png'
      )

However, the resulting image clearly has whitespace:

(It's easier to see from this link against a non-white background.)

2 Likes

Not that it answers the question, but I think there's whitespace at the bottom of the larger image as well:

Plausible but totally untested hypothesis: this could actually be the same amount (vertical height) of whitespace, which, obviously, looks bigger on the favicon:
image

1 Like

Wow, thanks. That actually helps a lot -- gives me a whole new direction for trouble-shooting. I'm not sure if I should feel dumber about not noticing that or the fact that I capitalized the first "G" in the subject.. :flushed:

1 Like

Blame auto-correct for the latter for sure! Plus, never dumb, only smarter now!

Hi,

I'm running into the same problem and I am wondering if you've found a solution for it or not yet?

Cheers,
Amir.

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ, linked to below.

I've been having this issue for a while now. Mostly when I use coord_equal() in my plots.
The problem is the following:

  • When running ggsave(), the default ratio is whatever the pane is currently set to, as far as I understand. ("...If not supplied, uses the size of current graphics device.") But this ratio rarely works, hence the whitespace. Here is a small example:
df <- tibble(x = c(1:10),
             z = c(21:30))
ggplot(df, aes(x=x, y=1, fill=z)) + 
  geom_tile() + 
  coord_equal()
ggsave("1.png")
Saving 10.2 x 6.71 in image

  • An alternative is to supply height and width, but there is no way of knowing the ratio in advance. All I end up doing is feeding ggsave various height/weight pairs until I figure out the ratio. It takes a while sometimes, and there is gotta be a better way.

One hack I found on SO is here: r - Save plot with a given aspect ratio - Stack Overflow
But it seems like an overkill for something as simple as extracting a plot ratio and feeding it back into ggsave.

Is there a known solution to this, besides the abovementioned SO solution?

2 Likes