1

I'm trying to get 300 dpi images with ggsave, and am only getting the default 72 dpi.

Here's an example of the code I'm using:

library(tidyverse)
x <- rnorm(100, 5, 1)
x <- tibble(x)
ggplot(x, aes(x = x)) + geom_histogram()
ggsave("hist.png", width = 10, height = 10, units = "in", dpi = 300)

When I run this I get a 72 dpi .png file. I'm using a MAC with Catalina 10.15.7 and updated to the most recent versions of R and ggplot2 today.

Peter Miksza
  • 347
  • 3
  • 11
  • if you switch the dpi does the final resolution change? just curious to see if it responds at all to that input – Mike Dec 10 '21 at 18:57
  • according to the "inspector" in Mac Preview, It changes the image size in pixels, but not the dpi is still 72. For example, 300 dpi yields 3000 X 3000 pixels; whereas, 400 dpi yields 4000 X 4000 pixels. – Peter Miksza Dec 10 '21 at 19:01
  • A [previous post](https://stackoverflow.com/q/63935939/5325862) asking the same thing didn't get an answer but has some comments that might help. You might also want to specify a graphics device, such as cairo or agg, and pass its specific arguments. For example, in debugging recently I found when I used agg I had to use a `res` argument, not `dpi` – camille Dec 10 '21 at 19:13
  • 2
    If you are getting 3000 X 3000 10inch picture with 300dpi, and 4000 X 4000 10inch picture with 400dpi export, that would be correct. So what makes you certain that you are not getting anything other than 72dpi – CALUM Polwart Dec 10 '21 at 23:33
  • See [this question](https://graphicdesign.stackexchange.com/questions/107888/check-dpi-of-png-file) which points out that DPI isn't really an inherent property of the image file itself. Changing the DPI just changes that total number of pixel created. It's up to the program you are going to use to print the image to determine how many pixels to print in an inch. – MrFlick Dec 11 '21 at 00:09
  • 2
    A useful recent blog post explaining resolution with ggplot2: https://www.christophenicault.com/post/understand_size_dimension_ggplot2/ – Jon Spring Dec 11 '21 at 00:37

1 Answers1

2

I've got the same issue, followed @Jon Spring url and everything was fixed

After checking each image using inspector, my experience so far was:

# Mixed results
ggsave("img_ggsave.tiff", img,width = 6,height=7, dpi=300, units = "in")

# Does not work for mac
tiff("img_native.tiff", width = 6, height = 7, units = "in", res = 300)
img
dev.off()

# ragg always works for mac
ragg::agg_tiff("img_ragg.tiff", width = 6, height = 7, units = "in", res = 300)
img
dev.off()