wilkelab/cowplot

`draw_image` only works when background themes are set to `ggplot2::element_blank()`

Closed this issue · 1 comments

When following the tutorial to add logos to a ggplot, I noticed that the added image is only visible when the ggplot you create uses the cowplot::theme_half_open or you specify blank background themes. In the example below, you cannot see the logo when you use the default ggplot2 theme (as seen in p2). Would it be possible to apply the necessary theme changes when draw_plot is called so this functionality works with ggplot out of the box?

library(ggplot2)
library(cowplot)

logo_file <- system.file("extdata", "logo.png", package = "cowplot")
cow <- ggdraw() +
  draw_image(
    logo_file, scale = .7
  )


# Example from tutorial
p1 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.7) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  theme_half_open(12)

# Make plot with no changes to theme.
p2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.7) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) 

# Remove background theme.
p3 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.7) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  theme(
    plot.background = element_blank(),
    panel.background = element_blank(),
    panel.grid = element_blank(),
    legend.background = element_blank()
  )

# Works as it does in the tutorial
cow + draw_plot(p1)

# Cannot see logo.
cow + draw_plot(p2)

# Can see logo. 
cow + draw_plot(p3)

Created on 2023-05-23 with reprex v2.0.2

Right after posting this, I realized I could just call draw_image after draw_plot as a workaround for my use case. Thanks for an awesome package!