Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing default ggsave background color

Tags:

r

ggplot2

I am having an issue where a few months ago, the behavior of ggsave() seemed to change, defaulting to a transparent background instead of a white background. For instance this code:

box_plot <-ggplot(vaccine_data_summary, aes(y = vaccine_uptake, x = region)) +
          geom_boxplot(outlier.shape = NA)

ggsave("box_plot.png", plot = box_plot,  width = 1200, height = 675, units = "px", scale  = 2) 

Results in a transparent background. To fix this I have been adding the argument for background.

ggsave("box_plot.png", plot = box_plot,  width = 1200, height = 675, units = "px", bg = "white", scale  = 2) 

However, I would prefer to set the default to white again so I wouldn't have to update all of my old code.

like image 930
Joe Avatar asked Jan 28 '26 07:01

Joe


1 Answers

You can override the ggsave function.

ggsave <- function(..., bg = 'white') ggplot2::ggsave(..., bg = bg)

Now when you call ggsave function it would use this function with default bg value as 'white'.

like image 91
Ronak Shah Avatar answered Jan 30 '26 00:01

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!