rggplot2ggsave

`ggsave` jpg reversed black-white values with `theme_minimal()`


When I save a ggplot image with theme_minimal the black and white values are reversed in a photo negative like effect. This does not occur if I do not use a theme nor does it occur with theme_bw. It also does not occur when saving to .pdf or .png. I have tested and this occurs when running in RStudio, R GUI, or through the terminal. I'm running R version 4.0.2 on Mac OS 10.15.7.

I would greatly appreciate any insight into debugging this. The behavior has persisted for several weeks across multiple full system restarts.

library(ggplot2) 
ggplot(diamonds, aes(x = cut, y = clarity)) + 
  geom_point() + 
  theme_minimal() 

ggsave("test_minimal.jpg")

plot saved as photo negative


Solution

  • Seems like 'theme_minimal' defaults to black background for jpg files (pdf and png were fine and I used Windows 10). @stefan had proposed two ways to overcome this in the comments above. I did not see that and went searching again. So posting the full solution here:

    library(ggplot2) 
    ggplot(diamonds, aes(x = cut, y = clarity)) + 
      geom_point() + 
      theme_minimal() 
    
    ggsave("test_minimal.jpg",bg="white")