ggsave() bug in ggplot2?
1
2
Entering edit mode
10 months ago
Medeea ▴ 20

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 • 2.4k views
ADD COMMENT
1
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

file("Figure1.jpg")

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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!

ADD REPLY
1
Entering edit mode

Hmm file command writes the dpi for files created with ggsave. Here are some examples: https://colab.research.google.com/drive/162WNVv_VvG2oqPmwesuIkaS_9oE5mQRd?usp=sharing

Are you on Mac by any chance? Maybe it is related to this: https://stackoverflow.com/questions/73664667/r-ggsave-dpi-is-always-72-in-macos

ADD REPLY
4
Entering edit mode
10 months ago

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.

ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
1
Entering edit mode

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()?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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!

ADD REPLY

Login before adding your answer.

Traffic: 2681 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6