2

I want to export a ggplot as JPEG with 300dpi for print but whatever I do I get a file with 72dpi (on macOS 10.15.6, RStudio 1.3.1073, R version 4.0.2).

I tried both methods as explained here: Saving a high resolution image in R but neither are working.

Here's some sample data:

x <- c("a", "b", "c")
y <- c(2, 3, 4)
df <- data.frame(x, y)
# ggplot
gg <- ggplot(df, aes(x=x, y=y)) + geom_point()
# method 1
print(gg)
ggsave("method1.jpg", dpi = 300)

# method 2
jpeg("method2.jpg", units = "in", width = 5, height = 5, res = 300)
print(gg)
dev.off()

Thanks in advance!

EDIT: I tried the same using a base R plot (as in boxplot(df$y)) but that didn't work either. EDIT2: I tried the same setting the size (as in ggsave('method1.jpg', width = 5, height = 5, dpi = 300)) which also didn't work

J. Ziegler
  • 73
  • 1
  • 1
  • 5
  • Have you tried setting the dimensions and size in method 1 like you did in method 2? ```ggsave('method1.jpg', width = 5, height = 5, dpi = 300)``` – Fnguyen Sep 17 '20 at 10:20
  • yes, that didn't work either... – J. Ziegler Sep 17 '20 at 10:24
  • Okay then I would add that to your question, also if you have tried setting a dpi lower than 72 (so basically whether it doesn't scale at all). – Fnguyen Sep 17 '20 at 10:26
  • Thanks, I tried that! Seems like instead of changing the dpi it just changes the size. So when I set dpi = 300 I get an image size 1500x1500, when I set dpi to 72 I get an image size 360x360 but they are both technically 72dpi – J. Ziegler Sep 17 '20 at 10:40
  • Update: I sent the image I get when I set dpi = 300 to a colleague who uses a Windows PC, and she says it's 96dpi. I don't get it? – J. Ziegler Sep 17 '20 at 10:44
  • It seems that this is a common issue without a clear solution in ggsave as it should simply work. Most solutions propose using a different graphic engine to save: https://www.r-bloggers.com/high-resolution-figures-in-r/ or https://community.rstudio.com/t/save-high-resolution-figures-from-r-300dpi/62016 – Fnguyen Sep 17 '20 at 10:45

0 Answers0