0

I tried several ways of saving my plots from R, but non of them work. Either they are blurry or the colors of the barplot change and the bars become somewhat transparent with an uneven color. The plots were created with ggplot2.

This is what I've tried so far:

ggsave(p, filename = "plot1.jpeg", width = 8, height = 6, dpi = 300, type = "cairo-png")
png("bp.png")
print(p)
dev.off()

And the third option was to just use the export option in the plot window.

It doesn't really matter what format I save them in (pdf, jpeg, png), they're all either blurry or the colors are changed. Right now I'm just taking screenshots of the plots which is obviously not the proper way to do it.
Down below you can see how the plots change after saving them:

R enter image description here

PNG enter image description here

Edward
  • 10,360
  • 2
  • 11
  • 26
Sophya
  • 11
  • 2
  • Welcome to SO, Sophya! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including an example code to create your plot and a snippet of your data or some fake data. Otherwise we can't reproduce your issue. – stefan Feb 28 '23 at 07:54
  • Why you filename is a jpeg if you want save your plot in .png ? Very strange. Try to directly save your plot in png. – Tribaldi Feb 28 '23 at 08:57

1 Answers1

1

I solved the problem! There seems to be an issue with some plots when working on a Mac. So I changed the settings for the graphics device. Now the resolution is great and colors stay the same after saving. These are the settings I used:

png(filename = "filename.png", width = 6800 , height = 5648, units = "px", type = "quartz", pointsize = 15, res = 800) print(p) dev.off()

Changing type = to "quartz" solved the problem on my Mac. This may change depending on the operating system of the computer. Other options to try would be: type = "cairo" , type = "cairo-png" or type = "Xlib"

To avoid the font being really small in the png I added pointsize =. Suitable dimensions may change depending on the plot.

Sophya
  • 11
  • 2