R/theme_jr.R

Defines functions theme_jr

Documented in theme_jr

#' Provides `theme_jr()` for `ggplot2`
#'
#' @param base_size base font size
#' @param base_family base font family (LM Sans 10)
#' @param base_line_size base size for line elements
#' @param base_rect_size base size for rect elements
#'
#' @return Theme for ggplot2
#' @importFrom ggplot2 element_blank element_line element_rect element_text margin rel theme unit
#'
#' @examples
#' df <- data.frame(x = runif(10), y = rnorm(10))
#' require(ggplot2)
#' ggplot(df, aes(x = x, y = y)) + geom_point() + theme_jr()
#' @export
theme_jr <- function(base_size = 11, base_family = "",
                     base_line_size = base_size/22, base_rect_size = base_size/22) {
  half_line <- base_size/2
  theme(
    line = element_line(colour = "black", linewidth = base_line_size,
                        linetype = 1, lineend = "butt"),
    rect = element_rect(fill = "white", colour = "black",
                        linewidth = base_rect_size, linetype = 1),
    text = element_text(family = base_family, face = "plain",
                        colour = "black", size = base_size, lineheight = 0.9,
                        hjust = 0.5, vjust = 0.5, 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.8), colour = "black"),
    axis.text.x = element_text(margin = margin(t = 0.9 * 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.9 * 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 = "black"), axis.ticks.length = unit(half_line/2, "pt"),
    axis.title.x = element_text(margin = margin(t = 2*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 = 2*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, fill = 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 = "white", colour = "white"),
    legend.key.size = unit(1.25, "lines"),
    legend.key.height = NULL,
    legend.key.width = NULL,
    legend.text = element_text(size = rel(0.8)),
    legend.text.align = NULL,
    legend.title = element_text(hjust = 0),
    legend.title.align = 1,
    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_rect(fill = NA, colour = "black",
                                linewidth = 0.5),
    panel.grid = element_line(colour = NA),
    panel.grid.major = element_line(colour = NA),
    panel.grid.minor = element_line(colour = NA),
    panel.spacing = unit(half_line, "pt"),
    panel.spacing.x = NULL,
    panel.spacing.y = NULL,
    panel.ontop = FALSE,
    strip.background = element_rect(colour = NA, fill = NA),
    strip.text = element_text(colour = "black", 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)
}
julianre/jRmisc documentation built on June 4, 2024, 11:32 p.m.