Mikata-Project / ggthemr

Themes for ggplot2.
886 stars 107 forks source link

Set transparent as custom colour? #10

Closed ekstroem closed 7 years ago

ekstroem commented 7 years ago

I'm trying to create a custom theme that can be used with ggthemr. Is it possible to set transparent colours - in particular for the background. I tried somthing along the following lines, but transparent is not a known R colour.

mytheme <- define_palette(swatch = c('black', 'red', 'green', 'blue', 'brown', 'purple', 'yellow'), gradient = c(lower = 'red', upper = 'green'),
                        background = "transparent", 
                        text = c("#444444", "#444444"), 
                        line = c("#6e6e6e", "#6e6e6e"), 
                        gridline = "#c3c3c3")

In ggplot it is possible to set the background using, say, something along the following lines

opts(
    panel.background = theme_rect(fill = "transparent",colour = NA), # or theme_blank()
    plot.background = theme_rect(fill = "transparent",colour = NA)
)

Thanks for a wonderful package!

cttobin commented 7 years ago

If you set type to outer it should work:

mytheme <- define_palette(
  swatch = c('black', 'red', 'green', 'blue', 'brown', 'purple', 'yellow'), 
  gradient = c(lower = 'red', upper = 'green'),
  background = "transparent", 
  text = c("#444444", "#444444"), 
  line = c("#6e6e6e", "#6e6e6e"), 
  gridline = "#c3c3c3"
)

ggthemr(mytheme, type = 'outer')

The default for type is inner which then always sets the plot.background to white. But if type is set to outer, plot.background shares the same colour as panel.background (i.e. transparent in this case).

That isn't obvious at all sorry, but I'd never actually considered transparent backgrounds. I should probably change it so that it checks for the special case of background being transparent, and then ignore the type parameter.

I'll close this but let me know if that doesn't work for whatever reason.