R/themes.R

Defines functions theme_vmisc theme_blog theme_pub theme_poster theme_beamer

Documented in theme_beamer theme_blog theme_poster theme_pub theme_vmisc

#' A base ggplot2 theme used by other themes in the vmisc package
#'
#' A clean theme with inward tickmarks, (optional) dotted gridlines and
#' a few other common options. Based on [ggplot2::theme_grey()].
#'
#' @param base_size Text size (defaults to 12).
#' @param base_family Text font family (defaults to Helvetica).
#' @param legend Position of legend. One of `"top"`, `"bottom"`,
#'   `"left"`, `"right"`, or `"none"` (default).
#' @param grid Display (major) gridlines? Defaults to FALSE.
#' @param facet_label Display facet labels? Defaults to FALSE.
#'
#' @export
#' @examples
#' library(ggplot2)
#' library(vmisc)
#' p <- ggplot(mtcars, aes(x = mpg, y = wt)) +
#'      geom_point()
#' p + theme_vmisc()

theme_vmisc <- function(base_size = 12,
                        base_family = "Helvetica",
                        legend = "none",
                        grid = FALSE,
                        facet_label = FALSE) {
  t <- theme_gray(
    base_size = base_size,
    base_family = base_family
  ) %+replace%
    theme(
      panel.background = element_rect(fill = "white"),
      panel.grid.minor = element_blank(),
      panel.border = element_rect(fill = NA, color = "grey50"),
      panel.spacing = unit(0.2, "cm"),
      axis.text.x = element_text(
        size = rel(.8),
        margin = unit(c(2, 0, 0, 0), "mm")
      ),
      axis.text.y = element_text(
        size = rel(.8),
        margin = unit(c(0, 2, 0, 0), "mm"),
        hjust = 1
      ),
      axis.ticks = element_line(size = .3),
      axis.ticks.length = unit(-1, "mm"),
      plot.margin = unit(c(10, 10, 10, 10), "pt"),
      legend.key = element_rect(fill = "white", color = NA),
      legend.position = "none",
      panel.grid.major = element_blank(),
      strip.text = element_blank()
    )
  if (legend != "none") t <- t + theme(legend.position = legend)
  if (grid) t <- t + theme(
      panel.grid.major = element_line(linetype = 3, size = .2, color = "grey50")
      )
  if (facet_label) {
    t <- t + theme(
        strip.background = element_rect(colour = "#004276", fill = "#004276"),
        strip.text.x = element_text(size = rel(0.9), color = "white",
                                    margin = margin(4,4,4,4, "pt"), face = "bold"))
  }

  return(t)
}

#' A ggplot2 theme used in my blog <https://mvuorre.github.io>
#'
#' Just a clean ggplot2 theme with sensible defaults for online presentation,
#' based on [vmisc::theme_vmisc()].
#'
#' @param base_size Text size (defaults to 12).
#' @param base_family Text font family (defaults to Helvetica).
#'
#' @export
theme_blog <- function(base_size = 12, base_family ="Helvetica") {
  .Deprecated("theme_vmisc")
  theme_vmisc(grid = TRUE)
}

#' A clean ggplot2 theme that could be used for manuscripts
#'
#' Just a clean ggplot2 theme with sensible defaults for journal articles,
#' based on [vmisc::theme_vmisc()].
#'
#' @param base_size Text size (defaults to 12).
#' @param base_family Text font family (defaults to Helvetica).
#'
#' @export
theme_pub <- function(base_size = 12, base_family = "Helvetica") {
  .Deprecated("theme_vmisc")
  theme_gray(base_size = base_size, base_family = base_family) +
    theme(
      axis.text.x = element_text(size = rel(0.8), margin = unit(c(2, 0, 0, 0), "mm")),
      axis.text.y = element_text(
        size = rel(0.8),
        margin = unit(c(0, 2, 0, 0), "mm"),
        hjust = 1
      ),
      axis.ticks = element_line(size = .3),
      axis.ticks.length = unit(-.7, "mm"),
      legend.position = "none",
      panel.background = element_rect(fill = NA),
      panel.border = element_rect(fill = NA, colour = "grey30", size = .60),
      panel.grid.major = element_line(
        linetype = "dotted",
        size = .15, colour = "grey73"
      ),
      panel.grid.minor = element_blank(),
      panel.spacing = unit(1, "mm"),
      strip.background = element_blank(),
      strip.text.x = element_blank(),
      plot.margin = unit(c(12, 12, 12, 12), "pt")
    )
}

#' A ggplot2 theme used in my 2016 Psychonomics poster
#' <https://osf.io/92fba/>
#'
#' Just a clean ggplot2 theme with sensible defaults for research posters,
#' based on [theme_grey()].
#'
#' @param base_size Text size (defaults to 12).
#' @param base_family Text font family (defaults to Helvetica).
#'
#' @export
theme_poster <- function(base_size = 12, base_family ="Helvetica") {
  .Deprecated("theme_vmisc")
  theme_gray(base_size = base_size, base_family = base_family) +
    theme(
      axis.text.x = element_text(
        size = rel(0.8),
        margin = unit(c(2, 0, 0, 0), "mm")
      ),
      axis.text.y = element_text(
        size = rel(0.8),
        margin = unit(c(0, 2, 0, 0), "mm"),
        hjust = 1
      ),
      axis.ticks = element_line(size = .3),
      axis.ticks.length = unit(-1, "mm"),
      legend.position = "bottom",
      legend.key = element_rect(fill = NA),
      legend.background = element_rect(fill = alpha("white", .15)),
      panel.background = element_rect(fill = "white"),
      panel.border = element_rect(colour = "grey30", fill = NA, size = .60),
      panel.spacing = unit(0.1, "cm"),
      panel.grid.minor = element_blank(),
      panel.grid.major = element_line(
        linetype = 3, size = .3,
        color = "gray50"
      ),
      plot.background = element_rect(
        fill = "transparent",
        colour = "transparent"
      ),
      plot.caption = element_text(size = rel(.70), face = "italic"),
      plot.subtitle = element_blank(),
      plot.title = element_text(hjust = 0, face = "bold"),
      strip.background = element_rect(colour = NA, fill = "#004276"),
      strip.text = element_text(size = rel(0.8), color = "white"),
      strip.text.x = theme_bw()$strip.text.x,
      text = element_text(colour = "black"),
      plot.margin = unit(c(12, 12, 12, 12), "pt")
    )
}

#' A ggplot2 theme for Beamer presentations
#'
#' Similar to [vmisc::theme_vmisc()], but colors match Beamer's Madrid theme
#' with the default color scheme, and sizes are adjusted.
#'
#' @param base_size Text size (defaults to 8).
#' @param base_family Text font family (defaults to Helvetica).
#' @param ... Passed to `theme_vmisc()`.
#'
#' @export
#' @examples
#' library(ggplot2)
#' library(vmisc)
#' p <- ggplot(mtcars, aes(x = mpg, y = wt)) +
#'      geom_point()
#' p + theme_beamer()
#'
theme_beamer <- function(base_size = 8, base_family ="Helvetica", ...) {
  theme_vmisc(base_size = base_size, base_family = base_family, ...) +
    theme(
      legend.justification = "left",
      legend.key.size = unit(.7, "lines"),
      legend.margin = margin(-6, 0, 0, 0, "pt"),
      panel.spacing = unit(0.1, "cm"),
      plot.background = element_rect(
        fill = "transparent",
        colour = "transparent"
      ),
      plot.caption = element_text(size = rel(.70), face = "italic"),
      plot.title = element_text(hjust = 0, face = "plain", size = rel(1)),
      strip.background = element_rect(colour = NA, fill = "#3336AC"),
      strip.text = element_text(size = rel(0.75), color = "white"),
      strip.text.x = element_text(margin = unit(c(3, 0, 3, 0), "pt")),
      text = element_text(colour = "black"),
      plot.margin = unit(c(8, 8, 8, 8), "pt")
    )
}
mvuorre/vmisc documentation built on May 23, 2019, 10:56 a.m.