Changing background of patchwork plot

Hi, I have made two separate box plots using ggplot, both of these have no background. When I then apply patchwork to store them side-by-side in an individual plot, the background becomes white. How can I change this? I want there to be no background color. I do want the grid-lines from the plots, just not the white square behind it :slight_smile:

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars, aes(mpg, hp)) +
  geom_smooth(method = "lm") +
  theme_minimal() +
  theme(
    panel.background = element_rect(fill = "transparent"), # Transparent panel background
    plot.background = element_rect(fill = "transparent", color = NA), # Transparent plot background
    legend.background = element_rect(fill = "transparent")
  ) # Transparent legend background

p1 <- ggplot(mtcars, aes(mpg, drat)) +
  geom_smooth(method = "lm") +
  theme_minimal() +
  theme(
    panel.background = element_rect(fill = "transparent"), # Transparent panel background
    plot.background = element_rect(fill = "transparent", color = NA), # Transparent plot background
    legend.background = element_rect(fill = "transparent")
  ) # Transparent legend background

# this works
ggsave("plot_transparent_background.png", p1, bg = "transparent")

# this doesn't
p <- p1 + p2

ggsave("plot_transparent_background.png", p, bg = "transparent")

Thank you for the reply!

Is there any way to save both plots as one .png with a transparent background?
As in, save the "p" plot with transparent background.

This will require tinkering.

library(ggplot2)
library(magick)
#> Linking to ImageMagick 6.9.12.93
#> Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fftw, ghostscript, x11

p1 <- ggplot(mtcars, aes(mpg, hp)) +
  geom_smooth(method = "lm") +
  theme_minimal() +
  theme(
    panel.background = element_rect(fill = "transparent"), # Transparent panel background
    plot.background = element_rect(fill = "transparent", color = NA), # Transparent plot background
    legend.background = element_rect(fill = "transparent")
  ) # Transparent legend background

p2 <- ggplot(mtcars, aes(mpg, drat)) +
  geom_smooth(method = "lm") +
  theme_minimal() +
  theme(
    panel.background = element_rect(fill = "transparent"), # Transparent panel background
    plot.background = element_rect(fill = "transparent", color = NA), # Transparent plot background
    legend.background = element_rect(fill = "transparent")
  ) # Transparent legend background

ggsave("/Users/ro/Desktop/plot_transparent_backgroundleft.png", p1, bg = "transparent")
#> Saving 7 x 5 in image
#> `geom_smooth()` using formula = 'y ~ x'
ggsave("/Users/ro/Desktop/plot_transparent_backgroundright.png", p2, bg = "transparent")
#> Saving 7 x 5 in image
#> `geom_smooth()` using formula = 'y ~ x'

# stitching

left  <- image_read("/Users/ro/Desktop/plot_transparent_backgroundleft.png")
right <- image_read("/Users/ro/Desktop/plot_transparent_backgroundright.png")
stuck <- image_append(c(left,right))
image_write(stuck,"/Users/ro/Desktop/stuck.png","png")

Created on 2023-11-26 with reprex v2.0.2

cut-and-paste of image as saved (transparent background not apparent in web rendering but can be seen in photo editor)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.