Inherit data in ggplot2 theme


#1

Hi there,

I was wondering if there is any possibility to have a conditional theme for ggplot2.

I have created a theme for my package to visualize results, which can either be continuous or discrete.
It returns a list with the theme and a color scale (viridis in this case), like so:

theme_test <- function() {
 
  # start with minimal theme
  ret <-
    ggplot2::theme_minimal()
  
  # extend it
  theme_base <- ret + ggplot2::theme()
  
  # define color scale
  theme_color <- viridis::scale_fill_viridis()
  
  # return as list
  list(theme_base,
       theme_color)
  
}

The scale should then either be discrete or continuous, depending on the input data and without the need to specify that in every case.

… is there any way to access the inherited data with the . operator?
Maybe as a function in the theme arguments (like waiver?) or in the function declaration of theme_test?


#2

:pager: @thomasp85 , feel like you’d know if it’s possible… You can yes or no it, and I’m happy to dig, if yes.


#3

AFAIK it is a no… :confused:


#4

OK, thanks anyway :slight_smile:


#5

why do you want to mix between the theme and the scale? they are two different objects. you can make a separate function to handle the discrete/continuous switch indep. of theme setting


#6

I thought it would be more convenient for any user to use it like this.
There are a couple of packages doing that, is there any reason to strictly do it sequentially?


#7

i dont think sequentially is at issue. they are two different functionalities that deal with different ggplot domains. colour scales are layer specific, where themes are plot layout specific.