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.
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.
Can you run the command
file Figure1.jpg
and 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
file
which 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
file
command 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