Jake L
Jake L

Reputation: 1067

How to preserve colors saving from ggplot?

This may not be the right place to ask this question, but I'm having trouble saving my colors from ggplot. I made this plot (pic below), and used ggsave() to save it as a .png file, and I loved the way it looked. However, when I take that .png file and upload it anywhere (specifically, in this case, to twitter and UpWork), the colors distort. The blue and orange get much darker, and I like the plot much less. Why is this happening? Is it the way I'm saving? Is it a function of file compression on those websites?

Can anyone recommend a better way to save that will not influence the aesthetics of my plots?

Attached here are screenshots of what you can see on the file on my computer (first pic), and a screenshot of the uploaded version of that same exact file (second pic. Darker). Hopefully they both upload as they look to my computer here...

Here is an example of the code/colors I am using:

require(ggplot2)
plot <- ggplot(data=data.frame(x=c(1:3),y=c(1:3)),
       aes(x=x,y=y))+
  geom_point(col="#E56800", size=3)+
  theme_classic()+
  theme(panel.background = element_blank(),
        plot.background = element_rect(fill = "#354154"),
        text= element_text(color="#FCFFF9"),
        axis.text = element_text(color="#FCFFF9"))

ggsave(plot, filename = "plot.png",
       width = 5, height = 7,
       dpi=300)

enter image description here

enter image description here

EDIT: By the way, I'm using RStudio on a Macbook Pro, in case that's relevant. I always get confused by the graphical device options, so I'm guessing they have something to do with this.

Upvotes: 1

Views: 1240

Answers (1)

Jrm_FRL
Jrm_FRL

Reputation: 1423

You can try to install the CRAN Cairo package, and add a type argument in ggsave like this:

ggsave(plot, filename = "plot.png",
       width = 5, height = 7,
       dpi=300,
       type = "cairo-png") # add this argument

Cairo allows to export anti-aliased images (this is the default on Mac but not on PC), maybe this could help.

Upvotes: 1

Related Questions