R/ggplot2_theme.R

Defines functions theme_abdisttools_tufte theme_abdisttools_dark theme_abdisttools_light .onLoad

Documented in theme_abdisttools_dark theme_abdisttools_light theme_abdisttools_tufte

#' Tufte-inspired abdisttools ggplot2 theme
#'
#' Tufte-inspired ggplot2 theme
#'
#' @param base_size Default font size = 15
#' @param base_family Font family. Defaults to "serif"
#' @param base_line_size defaults to base_size/24
#' @param base_rect_size defaults to base_size/24
#' @export
#' @examples
#' ggplot2::theme_set(theme_abdisttools_tufte())
theme_abdisttools_tufte <-
  function(base_size = 15, base_family = "serif", base_line_size = base_size / 24,
             base_rect_size = base_size / 24) {
    half_line <- base_size / 2
    theme_grey(
      base_size = base_size, base_family = base_family,
      base_line_size = base_line_size, base_rect_size = base_rect_size
    ) %+replace%
      theme(
        line = element_line(
          colour = "white", size = base_line_size,
          linetype = 1, lineend = "butt"
        ),
        axis.line = element_blank(),
        axis.text.x = element_text(size = base_size * .75, color = "black", lineheight = 0.9, family = "serif"),
        axis.text.y = element_text(size = base_size * .75, lineheight = 0.9, colour = "black", family = "serif"),
        axis.ticks = element_line(size = 0.32, colour = "black"),
        axis.title.x = element_text(size = base_size * .95, color = "black", margin = margin(5, 0, 0, 0), face = "bold", family = "serif"),
        axis.title.y = element_text(size = base_size * .95, color = "black", angle = 90, margin = margin(0, 5, 0, 0), face = "bold", family = "serif"),
        axis.ticks.length = unit(0.5, "lines"),
        legend.background = element_rect(color = NA, fill = "#fffff8"),
        legend.key = element_rect(color = "#fffff8", fill = "#fffff8"),
        legend.key.size = unit(.5, "lines"),
        legend.key.height = NULL, 
        legend.key.width = NULL,
        legend.text = element_text(size = base_size  * 0.80, color = "black", family = "serif"),
        legend.title = element_text(size = base_size * 0.88, face = "bold", hjust = 0, color = "black", family = "serif"),
        legend.position = "right",
        legend.text.align = NULL,
        legend.title.align = NULL,
        legend.direction = "vertical",
        legend.box = NULL,
        panel.background = element_rect(fill = "#f9efde6D"),
        panel.border = element_rect(color = "white" , fill = NA),
        panel.grid = element_line(colour  = "white", linetype = "solid"),
        panel.grid.major = element_line(size = rel(1.15)),
        panel.grid.minor = element_line(size = rel(1.15)),
        panel.spacing = unit(0.5, "lines"),
        strip.text.x = element_text(color = "white", face = "bold", margin = margin(2.5, 0, 3.5, 0), family = "serif"),
        strip.text.y = element_text(color = "white", face = "bold", angle = -90, family = "serif"),
        plot.background = element_rect(colour = "#fffff5DA", fill = "#fffff5DA"),
        plot.title = element_text(size = base_size * 1.25, color = "black", family = "serif", face = "bold", hjust = 0, vjust = 1.325, margin = margin(b = half_line)),
        plot.subtitle = element_text(hjust = 0, vjust = 1.0, size = base_size * 1.15, margin = margin(b = half_line)),
        plot.margin = unit(rep(.70, 4), "lines"),
        strip.text = element_text(size = rel(0.82), margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)),
        strip.placement = "inside", 
        strip.placement.x = NULL,
        strip.placement.y = NULL, 
        strip.switch.pad.grid = unit(half_line / 2,"pt"), 
        strip.switch.pad.wrap = unit(half_line / 2,"pt"),
        strip.background = element_rect(fill = "#AD896B", color = "black")
      )
  }

#' Dark abdisttools ggplot2 theme
#'
#' Dark abdisttools ggplot2 theme
#'
#' @param base_size Default font size = 14
#' @param base_family Font family. Defaults to "serif"
#' @param base_line_size defaults to base_size/22
#' @param base_rect_size defaults to base_size/22
#' @export
#' @examples
#' ggplot2::theme_set(theme_abdisttools_dark())
theme_abdisttools_dark <- function(base_size = 14, base_family = "serif", base_line_size = base_size / 24, base_rect_size = base_size / 24) {
  theme_grey(base_size = base_size, base_family = base_family, base_line_size = base_line_size, base_rect_size = base_rect_size) %+replace%
    theme(
      line = element_line(colour = "white", size = base_line_size, linetype = 1, lineend = "butt"),
      axis.line = element_blank(),
      axis.text.x = element_text(size = base_size *
        0.8, color = "white", lineheight = 0.9), axis.text.y = element_text(size = base_size *
        0.8, color = "white", lineheight = 0.9), axis.ticks = element_line(
        color = "white",
        size = 0.2
      ), axis.title.x = element_text(
        size = base_size,
        color = "white", margin = margin(8, 0, 0, 0)
      ), axis.title.y = element_text(
        size = base_size,
        color = "white", angle = 90, margin = margin(
          0, 8,
          0, 0
        )
      ), axis.ticks.length = unit(0.3, "lines"),
      legend.background = element_rect(color = NA, fill = "black"),
      legend.key = element_rect(color = "white", fill = "black"),
      legend.key.size = unit(1.2, "lines"), legend.key.height = NULL,
      legend.key.width = NULL, legend.text = element_text(size = base_size *
        0.8, color = "white"), legend.title = element_text(size = base_size *
        0.8, face = "bold", hjust = 0, color = "white"),
      legend.position = "right", legend.text.align = NULL,
      legend.title.align = NULL, legend.direction = "vertical",
      legend.box = NULL, panel.background = element_rect(
        fill = "black",
        color = NA
      ), panel.border = element_rect(
        fill = NA,
        color = "white"
      ), panel.grid.major = element_line(color = "#7a8a9366"),
      panel.grid.minor = element_line(color = "#7a8a9366"),
      panel.spacing = unit(0.5, "lines"), strip.background = element_rect(
        fill = "grey30",
        color = "grey10"
      ), strip.text.x = element_text(size = base_size *
        0.8, color = "white", margin = margin(
        3, 0, 4,
        0
      )), strip.text.y = element_text(size = base_size *
        0.8, color = "white", angle = -90), plot.background = element_rect(
        color = "black",
        fill = "black"
      ), plot.title = element_text(size = base_size *
        1.2, color = "white"), 
      plot.margin = unit(rep(
        1,
        4
      ), "lines"),
      plot.tag = element_text(
        size = rel(1.2), hjust = 0.5,
        vjust = 0.5
      ), 
      plot.tag.position = "topleft"
    )
}

#' Light abdisttools ggplot2 theme
#'
#' Light abdisttools ggplot2 theme
#'
#' @param base_size Default font size = 13
#' @param base_family Font family. Defaults to "serif"
#' @param base_line_size defaults to base_size/22
#' @param base_rect_size defaults to base_size/22
#' @export
#' @examples
#' ggplot2::theme_set(theme_abdisttools_light())
theme_abdisttools_light <-
  function(base_size = 13, base_family = "serif", base_line_size = base_size / 24,
             base_rect_size = base_size / 24) {
    half_line <- base_size / 2
    theme(
      line = element_line(
        colour = "black", size = base_line_size,
        linetype = 1, lineend = "butt"
      ), rect = element_rect(
        fill = "white",
        colour = "black", size = base_rect_size, linetype = 1
      ),
      text = element_text(
        family = base_family, face = "plain",
        colour = "black", size = base_size, lineheight = 0.95,
        hjust = 0.54, vjust = 0.54, angle = 0, margin = margin(),
        debug = FALSE
      ), axis.line = element_blank(), axis.line.x = NULL,
      axis.line.y = NULL, axis.text = element_text(
        size = rel(0.84),
        colour = "grey30"
      ), axis.text.x = element_text(margin = margin(t = 0.8 *
        half_line / 2), vjust = 1), axis.text.x.top = element_text(margin = margin(b = 0.8 *
        half_line / 2), vjust = 0), axis.text.y = element_text(margin = margin(r = 0.8 *
        half_line / 2), hjust = 1), axis.text.y.right = element_text(margin = margin(l = 0.8 *
        half_line / 2), hjust = 0), axis.ticks = element_line(colour = "grey20"),
      axis.ticks.length = unit(half_line / 2, "pt"), axis.title.x = element_text(
        margin = margin(t = half_line / 2),
        vjust = 1
      ), axis.title.x.top = element_text(
        margin = margin(b = half_line / 2),
        vjust = 0
      ), axis.title.y = element_text(
        angle = 90,
        margin = margin(r = half_line / 2), vjust = 1
      ), axis.title.y.right = element_text(
        angle = -90,
        margin = margin(l = half_line / 2), vjust = 0
      ), legend.background = element_rect(colour = NA),
      legend.spacing = unit(2 * half_line, "pt"), legend.spacing.x = NULL,
      legend.spacing.y = NULL, legend.margin = margin(
        half_line,
        half_line, half_line, half_line
      ), legend.key = element_rect(
        fill = "grey95",
        colour = "white"
      ), legend.key.size = unit(1, "lines"),
      legend.key.height = NULL, legend.key.width = NULL, legend.text = element_text(size = rel(0.7)),
      legend.text.align = NULL, legend.title = element_text(hjust = 0),
      legend.title.align = NULL, legend.position = "bottom",
      legend.direction = NULL, legend.justification = "center",
      legend.box = NULL, legend.box.margin = margin(
        0, 0, 0,
        0, "cm"
      ), legend.box.background = element_blank(),
      legend.box.spacing = unit(2 * half_line, "pt"), panel.background = element_rect(
        fill = "white",
        colour = NA
      ), panel.border = element_blank(), panel.grid = element_line(colour = "#d9eefcA3"),
      panel.grid.minor = element_line(size = rel(0.5)), panel.spacing = unit(
        half_line,
        "pt"
      ), panel.spacing.x = NULL, panel.spacing.y = NULL,
      panel.ontop = FALSE, strip.background = element_rect(
        fill = "grey85",
        colour = NA
      ), strip.text = element_text(
        colour = "grey10",
        size = rel(0.8), margin = margin(
          0.8 * half_line,
          0.8 * half_line, 0.8 * half_line, 0.8 * half_line
        )
      ),
      strip.text.x = NULL, strip.text.y = element_text(angle = -90),
      strip.placement = "inside", strip.placement.x = NULL,
      strip.placement.y = NULL, strip.switch.pad.grid = unit(
        half_line / 2,
        "pt"
      ), strip.switch.pad.wrap = unit(
        half_line / 2,
        "pt"
      ), plot.background = element_rect(colour = "white"),
      plot.title = element_text(
        size = rel(1.2), hjust = 0,
        vjust = 1, margin = margin(b = half_line)
      ), plot.subtitle = element_text(
        hjust = 0,
        vjust = 1, margin = margin(b = half_line)
      ), plot.caption = element_text(
        size = rel(0.8),
        hjust = 1, vjust = 1, margin = margin(t = half_line)
      ),
      plot.tag = element_text(
        size = rel(1.2), hjust = 0.5,
        vjust = 0.5
      ), plot.tag.position = "topleft", plot.margin = margin(
        half_line,
        half_line, half_line, half_line
      ),


      complete = TRUE
    )
  }



.onLoad <- function(libname, pkgname) {
  library(ggplot2)
  ggplot2::theme_set(theme_abdisttools_dark())
  # cat(crayon::bold(crayon::blue("Setting ggplot2 theme to theme_abdisttools_dark")))
}
abnormally-distributed/abdisttools documentation built on May 5, 2019, 7:07 a.m.