R/theme_SLU.R

Defines functions theme_SLU

Documented in theme_SLU

#' SLU theme for ggplot2
#'
#' @param palette character or numeric defining the desired color palette
#' @param dark logical return theme for dark or light color scheme. Default dark.
#'
#' @return a ggplot theme using SLU colors
#' @export
#'
#' @examples
#' library(ggplot2)
#' p1 <- ggplot(iris, mapping = aes(x = Sepal.Width, y = Sepal.Length))
#'
#' p1 + labs(title = "Grey on green") +
#'   geom_point(color = SLUpalette("grey")["Glimmer"]) +
#'   theme_SLU("green")
theme_SLU <- function(palette, dark = TRUE) {
  if (!requireNamespace("ggplot2", quietly = TRUE)) {
    stop("ggplot2 required for theme_SLU to work",   call. = FALSE)
  }
  cols  <- SLUcolors::SLUpalette(palette, color_names = FALSE)
  greys  <- SLUcolors::SLUpalette("grey", color_names = FALSE)
  if (dark) {
    cols  <- rev(cols)
    greys  <- rev(greys)
  }
  ggplot2::theme(rect  = ggplot2::element_rect(fill = cols[5]),
        text = ggplot2::element_text(color = greys[2]),
        plot.title = ggplot2::element_text(hjust = 0.5), #argin = margin(b = -3), size = 9.6, face = "bold"),
        axis.text = ggplot2::element_text(color = greys[2]),
        axis.ticks = ggplot2::element_line(color = greys[2]),
        panel.grid.minor = ggplot2::element_line(color = NA),
        panel.border = ggplot2::element_rect(color = greys[1], fill = NA),
        panel.background = ggplot2::element_blank(), #ggplot2::element_rect(fill = cols[5]),
        panel.grid = ggplot2::element_line(color = NA))
}

#p1 <- ggplot(iris, mapping = aes(x = Sepal.Width, y = Sepal.Length)) +  labs(title = "Sepal plot") + geom_point()
kagervall/SLUcolors documentation built on Jan. 31, 2024, 11 a.m.