Search code examples
rmacosplotpngresolution

How to set the resolution of a png in R on a Mac


How to create a png with a 100 dpi resolution in R, on a Mac, by the png function?

I try:

png("1.png", res = 100)
plot(1)
dev.off()

On a Mac, the above code however produces a png with only 72 dpi, which can be checked e.g. on the command line by

sips -g dpiWidth 1.png
  dpiWidth: 72.000

Running the above R code on Windows or Unix, both results in a png with indeed the expected resolution of 100 dpi. I am aware of workarounds (using sips or ggplot), but I'd prefer the above command to work as expected on a Mac too. Am I missing something? Is there a way to (re)set the resolution of a yet existing png using R?

Thanks!


Solution

  • It seems that type = "cairo" can do the trick!

    png("1.png", res = 100, type = "cairo")
    plot(1)
    dev.off()
    

    On Mac's command line one can check that the resulting resolution is indeed 100 dpi:

    sips -g dpiWidth 1.png
      dpiWidth: 100.000