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.
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.
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.
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.
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.
Can you run the command
file Figure1.jpgand post the output?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
filewhich shows the resolution and dpi of the image file. ,:)Sorry, I was actually thinking that maybe you meant this
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!
Hmm
filecommand writes the dpi for files created with ggsave. Here are some examples: https://colab.research.google.com/drive/162WNVv_VvG2oqPmwesuIkaS_9oE5mQRd?usp=sharingAre 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
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.