R/liu_theme.R

Defines functions theme_liu

Documented in theme_liu

#' Set a ggplot2 theme for Linköping University
#'
#' @param liu_colors To use the colors of the university for various geoms
#'
#' @import ggplot2
#' 
#' @return a ggplot theme customized according to the Linköping University graphical manual
#' @export
#'
theme_liu <- function(liu_colors = TRUE){ 
  base_family <- "sans"
  
  liu_palette <- c("#00b9e7", "#ff6442", "#9d98de",
                   "#fdef5d", "#00ceb5", "#667e91",
                   "#93d9e1", "#ffb7ac", "#faf3a5")
  
  scale_colour_continuous <- function(...){
    scale_colour_gradient(..., low = "#d2eef9", high = "#17c7d2")
  }
  
  geom_list <- objects(envir = as.environment("package:ggplot2"), 
                       pattern = "^geom")
  geoms <- gsub("geom_", "", geom_list)
  geoms_to_drop <- c("bin2d", "count", "freqpoly", "histogram", "jitter", 
                     "qq", "qq_line", "sf_label", "sf_text")
  geoms <- geoms[-which(geoms %in% geoms_to_drop)]
  
  if(liu_colors){
    lapply(geoms, update_geom_defaults, 
           list(fill = "#00b9e7", colour = "#00b9e7"))  
    
    options(ggplot2.discrete.colour = liu_palette)
    options(ggplot2.discrete.fill = liu_palette)
    options(ggplot2.continuous.colour = scale_colour_continuous)
    options(ggplot2.continuous.fill = scale_colour_continuous)
    
  } else{
    lapply(geoms, update_geom_defaults, 
           list(fill = "grey35", colour = "black"))  
    options(ggplot2.discrete.colour = NULL)
    options(ggplot2.discrete.fill = NULL)
    options(ggplot2.continuous.colour = NULL)
    options(ggplot2.continuous.fill = NULL)  
  }
  
  list(
    theme() %+replace%   
      theme(
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_blank(),    
        panel.grid.minor = element_blank(),    
        axis.ticks = element_blank(),          
        axis.line = element_blank(),
        
        plot.title = element_text(family = base_family, size = 20, face = 'bold', 
                                  hjust = 0.5),             
        plot.subtitle = element_text(family = base_family, size = 14), 
        plot.caption = element_text(family = base_family, size = 10,  hjust = 1),             
        plot.background = element_rect(fill = "#93d9e1"),
        
        axis.title = element_text(family = base_family, 
                                  color = "black", face = "bold", size = 14),
        axis.title.x = element_text(vjust = -2),
        axis.title.y = element_text(hjust = -2),
        
        axis.text = element_text(family = base_family, 
                                 color = "black", size = 10),                
        
        legend.position = "right",
        legend.key = element_blank(),
        legend.title = element_text(family = base_family, face = "bold",
                                    color = "black", size = 14),
        legend.text = element_text(family = base_family, 
                                   color = "black", size = 12),
        legend.justification = "center",
        legend.direction = "vertical",
        legend.background = element_rect(fill = "white"),
        
        strip.background = element_rect(fill = "gray90"),
        strip.text = element_text(family = base_family, face = "bold", 
                                  color = "black", size = 14),
        panel.spacing = unit(1, "lines"),
        plot.margin = unit(c(1, 1, 1, 1), "lines"),
        complete = TRUE
      )
  )
}
CharlesTGurra/lab4RT documentation built on Sept. 25, 2020, 8:49 a.m.