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

1

8

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

Falls answered 5/9, 2021 at 23:50 Comment(6)
For a data point, I cannot reproduce this on win10, R-4.0.5, ggplot2-3.3.3.Elk
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))Heronry
Did a google search and found a similar issue with svg: github.com/tidyverse/ggplot2/issues/4212. In that post setting bg="white" in ggsave is proposed as a workaround.Heronry
@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.Falls
Thank you @Heronry both of those solutions have worked for me.Falls
I created a github issue here: github.com/tidyverse/ggplot2/issues/4604Falls
G
11

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")
Gaona answered 17/6, 2022 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.