Home > front end >  Can values of geom_function arguments (e.g. fill colours) be added to ggplot2 themes?
Can values of geom_function arguments (e.g. fill colours) be added to ggplot2 themes?

Time:02-08

For consistent data presentation I'm looking to create a new theme which can be used by anyone in my team using R to easily present plots in the same format. I'm using the stata theme as the base, and have so far been able to get the basic plot elements the way I want with the following;

theme_brand <- function(){ 
    font <- "Gotham"  
    
    theme_stata() % replace%    
    
    theme(
      plot.background = element_rect(fill = "white"),
 
      plot.title = element_text(
                    colour="#555862",
                    family = font,
                    size = 16),
     
      axis.title = element_text(             
                   family = font,            
                   size = 14,                
                   colour = "#555862"),      
      
      axis.text = element_text(              
                   family = font,            
                   size = 12),               
      axis.line = element_line (colour="#AAACB1"),
      axis.ticks.x = element_line(colour= "#AAACB1"),
      axis.ticks.y = element_line(colour= "#AAACB1"),
      panel.grid.major = element_line(colour = "#AAACB1"),
      panel.grid.minor = element_line(colour = "#D5D5D8"),
      panel.border = element_blank()
    )
}

What I would also like to do is set default colours, line styles etc for chart elements such as boxplots, labels and so on. While I've not stumbled on anything explicitly saying this isn't possible, I also haven't found anything showing how it can be done and I note that none of the themes supplied within ggthemes seem to have this functionality. So can it be done? And if not, is there an alternative way to ensure geom_function plots are presented consistently?

CodePudding user response:

You can’t add color palettes to themes, but you can change global defaults by setting a scale function or vector of colors via setOption(). e.g.:

setOption(“ggplot.fill.discrete”, scales::brewer_pal(palette = “Dark2”)(8))
setOption(“ggplot.color.continuous”, ggplot2::scale_color_viridis_c)
  •  Tags:  
  • Related