Skip to content

Commit

Permalink
themes_*() scale relevant elements with argument base_size. (#382)
Browse files Browse the repository at this point in the history
* `themes_*()` scale relevant elements with argument `base_size`.

* fix

* update tests

* update tests

* update tests

* update tests

* update tests

* Update theme_modern.R

* Update plot-compare-parameters-works.svg

* Allow to add global aes when plotting `visualosation_recipe()`

* update tests
  • Loading branch information
strengejacke authored Jan 31, 2025
1 parent 9fe3cc1 commit db4a812
Show file tree
Hide file tree
Showing 55 changed files with 6,720 additions and 6,007 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: see
Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2'
Version: 0.10.0.2
Version: 0.10.0.3
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# see (devel)

## Changes

* All `theme_*()` function get a `...` argument that is passed to
`ggplot2::theme()`.

* Arguments that change the size of elements in `theme_*()` functions (like
`plot.title.size` or `axis.text.size` now scale with the `base_size` argument,
to ensure proper scaling of plots when the base-size is changed.

# see 0.10.0

## Changes
Expand Down
41 changes: 22 additions & 19 deletions R/theme_abyss.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' A deep dark blue theme for ggplot.
#'
#' @inheritParams theme_modern
#' @inherit theme_modern note
#'
#' @examples
#' library(ggplot2)
Expand All @@ -12,23 +13,23 @@
#' geom_point(color = "white") +
#' theme_abyss()
#' @export
theme_abyss <-
function(base_size = 11,
base_family = "",
plot.title.size = 15,
plot.title.face = "plain",
plot.title.space = 20,
plot.title.position = "plot",
legend.position = "right",
axis.title.space = 20,
legend.title.size = 13,
legend.text.size = 12,
axis.title.size = 13,
axis.title.face = "plain",
axis.text.size = 12,
axis.text.angle = NULL,
tags.size = 15,
tags.face = "bold") {
theme_abyss <- function(base_size = 11,
base_family = "",
plot.title.size = 1.35 * base_size,
plot.title.face = "plain",
plot.title.space = 1.8 * base_size,
plot.title.position = "plot",
legend.position = "right",
axis.title.space = 1.8 * base_size,
legend.title.size = 1.2 * base_size,
legend.text.size = 1.1 * base_size,
axis.title.size = 1.2 * base_size,
axis.title.face = "plain",
axis.text.size = 1.1 * base_size,
axis.text.angle = NULL,
tags.size = 1.35 * base_size,
tags.face = "bold",
...) {
theme_modern(
base_size = base_size,
base_family = base_family,
Expand All @@ -51,10 +52,12 @@ theme_abyss <-
plot.background = element_rect(fill = "#001429"),
panel.background = element_rect(fill = "#001429"),
legend.background = element_rect(fill = "#001429"),
axis.line = element_line(color = "#f2f2f2"),
axis.line = element_line(color = "#f2f2f2", linewidth = base_size / 22),
rect = element_rect(colour = "#f2f2f2", linewidth = base_size / 22),
text = element_text(color = "#f2f2f2"),
axis.text = element_text(color = "#f2f2f2"),
panel.grid.major = element_line(color = "#465463"),
strip.text = element_text(color = "#f2f2f2")
strip.text = element_text(color = "#f2f2f2"),
...
)
}
41 changes: 22 additions & 19 deletions R/theme_blackboard.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' A modern, sleek and dark theme for ggplot.
#'
#' @inheritParams theme_modern
#' @inherit theme_modern note
#'
#' @examples
#' library(ggplot2)
Expand All @@ -12,23 +13,23 @@
#' geom_point(color = "white") +
#' theme_blackboard()
#' @export
theme_blackboard <-
function(base_size = 11,
base_family = "",
plot.title.size = 15,
plot.title.face = "plain",
plot.title.space = 20,
plot.title.position = "plot",
legend.position = "right",
axis.title.space = 20,
legend.title.size = 13,
legend.text.size = 12,
axis.title.size = 13,
axis.title.face = "plain",
axis.text.size = 12,
axis.text.angle = NULL,
tags.size = 15,
tags.face = "bold") {
theme_blackboard <- function(base_size = 11,
base_family = "",
plot.title.size = 1.35 * base_size,
plot.title.face = "plain",
plot.title.space = 1.8 * base_size,
plot.title.position = "plot",
legend.position = "right",
axis.title.space = 1.8 * base_size,
legend.title.size = 1.2 * base_size,
legend.text.size = 1.1 * base_size,
axis.title.size = 1.2 * base_size,
axis.title.face = "plain",
axis.text.size = 1.1 * base_size,
axis.text.angle = NULL,
tags.size = 1.35 * base_size,
tags.face = "bold",
...) {
theme_modern(
base_size = base_size,
base_family = base_family,
Expand All @@ -51,9 +52,11 @@ theme_blackboard <-
plot.background = element_rect(fill = "#0d0d0d"),
panel.background = element_rect(fill = "#0d0d0d"),
legend.background = element_rect(fill = "#0d0d0d"),
axis.line = element_line(color = "#E0E0E0"),
axis.line = element_line(color = "#E0E0E0", linewidth = base_size / 22),
rect = element_rect(colour = "#E0E0E0", linewidth = base_size / 22),
text = element_text(color = "#E0E0E0"),
axis.text = element_text(color = "#E0E0E0"),
strip.text = element_text(color = "#E0E0E0")
strip.text = element_text(color = "#E0E0E0"),
...
)
}
28 changes: 16 additions & 12 deletions R/theme_lucid.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' A light, clear theme for ggplot.
#'
#' @inheritParams theme_modern
#' @inherit theme_modern note
#'
#' @examples
#' library(ggplot2)
Expand All @@ -14,20 +15,21 @@
#' @export
theme_lucid <- function(base_size = 11,
base_family = "",
plot.title.size = 12,
plot.title.size = 1.1 * base_size,
plot.title.face = "plain",
plot.title.space = 15,
plot.title.space = 1.35 * base_size,
plot.title.position = "plot",
legend.position = "right",
axis.title.space = 10,
legend.title.size = 11,
legend.text.size = 10,
axis.title.size = 11,
axis.title.space = 0.9 * base_size,
legend.title.size = base_size,
legend.text.size = 0.9 * base_size,
axis.title.size = base_size,
axis.title.face = "plain",
axis.text.size = 10,
axis.text.size = 0.9 * base_size,
axis.text.angle = NULL,
tags.size = 11,
tags.face = "plain") {
tags.size = base_size,
tags.face = "plain",
...) {
theme_modern(
base_size = base_size,
base_family = base_family,
Expand All @@ -47,15 +49,17 @@ theme_lucid <- function(base_size = 11,
tags.face = tags.face
) +
theme(
axis.line.x = element_line(colour = "grey80"),
axis.line.y = element_line(colour = "grey80"),
axis.line.x = element_line(colour = "grey80", linewidth = base_size / 22),
axis.line.y = element_line(colour = "grey80", linewidth = base_size / 22),
rect = element_rect(colour = "#grey80", linewidth = base_size / 22),
axis.text = element_text(colour = "grey50"),
axis.title = element_text(colour = "grey30"),
strip.background = element_rect(colour = "grey70", fill = "grey90"),
strip.text = element_text(colour = "grey30"),
legend.title = element_text(colour = "grey30"),
legend.text = element_text(colour = "grey30"),
panel.grid.major = element_line(colour = "grey90"),
panel.grid.minor = element_line(colour = "grey92")
panel.grid.minor = element_line(colour = "grey92"),
...
)
}
57 changes: 32 additions & 25 deletions R/theme_modern.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
#' @param axis.text.angle Rotate the x axis labels.
#' @param tags.size Tags text size in pts.
#' @param tags.face Tags font face ("plain", "italic", "bold", "bold.italic").
#' @param ... Further arguments passed to `ggplot2::theme()`.
#'
#' @note Base elements like plot borders, titles etc. are scaling with
#' `base_size`, i.e. when `base_size` is increased, all other relevant elements
#' are increased in proportion to the base size, to ensure proper scaling of
#' the plot. Set arguments for the related elements explicitly to define custom
#' sizes.
#'
#' @examples
#' library(ggplot2)
Expand All @@ -33,37 +40,36 @@
#' @export
theme_modern <- function(base_size = 11,
base_family = "",
plot.title.size = 15,
plot.title.size = 1.35 * base_size,
plot.title.face = "plain",
plot.title.space = 20,
plot.title.space = 1.8 * base_size,
plot.title.position = "plot",
legend.position = "right",
axis.title.space = 20,
legend.title.size = 13,
legend.text.size = 12,
axis.title.size = 13,
axis.title.space = 1.8 * base_size,
legend.title.size = 1.2 * base_size,
legend.text.size = 1.1 * base_size,
axis.title.size = 1.2 * base_size,
axis.title.face = "plain",
axis.text.size = 12,
axis.text.size = 1.1 * base_size,
axis.text.angle = NULL,
tags.size = 15,
tags.face = "bold") {
tags.size = 1.35 * base_size,
tags.face = "bold",
...) {
# Remove legend title if necessary
if (is.null(plot.title.size)) {
plot.title.size <-
element_text(
size = plot.title.size,
face = plot.title.face,
margin = margin(0, 0, plot.title.space, 0)
)
plot.title.size <- element_text(
size = plot.title.size,
face = plot.title.face,
margin = margin(0, 0, plot.title.space, 0)
)
} else if (plot.title.size == "none") {
plot.title.size <- element_blank()
} else {
plot.title.size <-
element_text(
size = plot.title.size,
face = plot.title.face,
margin = margin(0, 0, plot.title.space, 0)
)
plot.title.size <- element_text(
size = plot.title.size,
face = plot.title.face,
margin = margin(0, 0, plot.title.space, 0)
)
}

# Remove legend title if necessary
Expand Down Expand Up @@ -94,10 +100,10 @@ theme_modern <- function(base_size = 11,
}

# Rotate
if (!is.null(axis.text.angle)) {
hjust <- 1
} else {
if (is.null(axis.text.angle)) {
hjust <- NULL
} else {
hjust <- 1
}

theme_classic(base_size = base_size, base_family = base_family) +
Expand All @@ -117,6 +123,7 @@ theme_modern <- function(base_size = 11,
axis.ticks = element_blank(),
plot.tag = element_text(size = tags.size, face = tags.face),
strip.background = element_blank(),
strip.text = element_text(face = "bold")
strip.text = element_text(face = "bold"),
...
)
}
28 changes: 19 additions & 9 deletions man/theme_abyss.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit db4a812

Please sign in to comment.