7

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

Lief Esbenshade
  • 793
  • 4
  • 13
  • For a data point, I cannot reproduce this on win10, R-4.0.5, ggplot2-3.3.3. – r2evans Sep 06 '21 at 00:00
  • 1
    I can reproduce this issue on my Mac. And had the same or at least similar issue this weekend when saving as png. My workaround was to add `theme(plot.background = element_rect(fill = "white", color = NA))` – stefan Sep 06 '21 at 00:27
  • 3
    Did a google search and found a similar issue with svg: https://github.com/tidyverse/ggplot2/issues/4212. In that post setting `bg="white"` in ggsave is proposed as a workaround. – stefan Sep 06 '21 at 01:25
  • @jared_mamrot Yes, when imported into powerpoint (or in my use case, overleaf) the image renders with a black background. Though not obvious from the toy example, in the case of a line plot the the trend lines are also appearing as white in the saved jpg. So its not just a case of no background. – Lief Esbenshade Sep 06 '21 at 02:34
  • Thank you @stefan both of those solutions have worked for me. – Lief Esbenshade Sep 06 '21 at 02:36
  • 1
    I created a github issue here: https://github.com/tidyverse/ggplot2/issues/4604 – Lief Esbenshade Sep 06 '21 at 16:52

1 Answers1

8

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")
Ajay
  • 184
  • 1
  • 7