This is a test version of Biostars. For the public version, visit https://www.biostars.org.
ggsave() bug in ggplot2?

Hi,

Example:

ggsave("Figure1.jpg", dpi = 300, width = 2500, height = 1500, units = "px")

The image is in fact saved at 96 dpi.

I have used R version 4.2.2 (changed to 4.2.3 as well) with ggplot2_3.4.2

This is a problem I've been experiencing only for the past few days.

r ggplot2

Can you run the command file Figure1.jpg and post the output?

file("Figure1.jpg")

A connection with
description "Figure1.jpg" class "file"
mode "r"
text "text"
opened "closed"
can read "yes"
can write "yes"

Oh, I meant the shell command file which shows the resolution and dpi of the image file. ,:)

Sorry, I was actually thinking that maybe you meant this

$ file Figure1.jpg
Figure1.jpg: JPEG image data, baseline, precision 8, 2500x1500, components 3

I don't see the dpi here, but if I right click on the image -> Size info: 2500x1500 717.6 KB 96 dpi 24 bit

Thank you!

Is it possible this is because you're saving it as a .jpg? Do you still get this error if you save as a .png?

yes, and also with .tiff

Later edit:

I used .png but changed units = "cm" instead of "px", as Matthias Zepper suggested bellow, and the file could be saved at 300dpi and it also looks better when I zoom into the plot.

1 answer

I wonder, what are you expecting? You are specifying the absolute number of pixels (units = "px") and getting an image output with exactly this number of pixels:

library(ggplot2)
ggplot() + stat_function(fun = dnorm, n = 50, color = "orange")

ggsave("Figure1.jpg", dpi = 300, width = 2500, height = 1500, units = "px")
ggsave("Figure1b.jpg", dpi = 150, width = 2500, height = 1500, units = "px")

Figure1 and Figure1b will have exactly the same number of pixels.

Specify a desired print output size in e.g. cm or inches and the dpi setting will be used to determine the required number of pixels to achieve this pixel density in the output once it is printed in that size. So Figure 2b will have double the number of pixels than Figure2:

ggsave("Figure2.jpg", dpi = 300, width = 25, height = 10, units = "cm")
ggsave("Figure2b.jpg", dpi = 150, width = 25, height = 10, units = "cm")

But a .jpg has no print size associated with it - just the number of pixels. You can print it at any size, but the pixel density will of course vary. 96dpi are the default in Windows.

Thank you very much for your suggestion, I have tried with units = "cm" and still it is not saved at desired dpi. I also ran your example and all figures are still saved at 96dpi.

Try PDF. You won't need dpi for that. Also, do things work well if you use the lower level png(); ggplot(); dev.off(); sequence instead of ggsave()?

Yes, ggsave("Figure1.pdf") works very good as well!

I also ran your example and all figures are still saved at 96dpi.

They are neither saved at 96dpi nor at any other resolution. They are saved as raster graphic having a certain number of pixels.

Once you print this graphic, the dpi of your printed image will be determined by the capabilities of your printer. All you need to ensure is, that your jpg or png has more than enough pixels for the print size you wish to produce later.

Even if your printer is capable of printing say 600dpi, if you provide it with a raster graphic that has too little pixels for a 200ppi - 600ppi resolution, the output will look crappy.

Try PDF. You won't need dpi for that.

Cave! For most figures, it indeed creates a vector graphic, but there are ggplot2 layers that do not support vector output. Generally, vector output is preferable, because it scales nicely and smoothly, but one has to pay more attention to the contents of the figure. If one e.g. plots a dense point cloud with geom_point(), every single point is retained, even when the individual point is not shown because of overplotting. A vector graphic with say 1e5 points has a significant file size and loads slowly.

I wonder how PDF worked for them when another vector format (tiff) failed.

I think the issue is solved with saving the file as .png There must be a problem with the .jpg format then.

Thanks everyone for the valuable input, I learn everyday from the community!

Log in to answer this question.