Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading