| Title: | Publication-Quality 'ggplot2' Geom Width |
|---|---|
| Description: | Width helper functions for publication-quality 'ggplot2' visualisation. These functions make it easier to create geoms such as bars that have a consistent width appearance across plots. |
| Authors: | David Hodge [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-3868-7501>) |
| Maintainer: | David Hodge <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-06-03 10:42:10 UTC |
| Source: | https://github.com/davidhodge931/ggwidth |
Standardise the width in 'ggplot2' geoms to appear visually consistent across plots with different numbers of categories, panel dimensions, and orientations.
This can be used in geoms such as ggplot2::geom_bar()/ggplot2::geom_col(), ggplot2::geom_boxplot(), ggplot2::geom_errorbar().
get_width( ..., n = NULL, n_dodge = NULL, orientation = c("x", "y"), equiwidth = NULL, panel_widths = NULL, panel_heights = NULL )get_width( ..., n = NULL, n_dodge = NULL, orientation = c("x", "y"), equiwidth = NULL, panel_widths = NULL, panel_heights = NULL )
... |
Must be empty. Forces all other arguments to be named and allows trailing commas. |
n |
Number of categories in the orientation aesthetic (i.e. |
n_dodge |
Number of dodge categories. Must match the number of groups in
the |
orientation |
Orientation: |
equiwidth |
Numeric. Scaling factor that controls the width appearance.
A value of |
panel_widths |
A |
panel_heights |
A |
A numeric width value passed to the width argument of
geom_bar(), geom_col(), or similar geoms.
library(ggplot2) library(dplyr) library(patchwork) set_theme( theme_grey() + theme(panel.widths = rep(unit(75, "mm"), 2)) + theme(panel.heights = rep(unit(50, "mm"), 2)) ) set_equiwidth(1) p1 <- mpg |> ggplot(aes(x = drv)) + geom_bar( width = get_width(n = 3), colour = "black", fill = "grey", ) p2 <- diamonds |> ggplot(aes(x = color)) + geom_bar( width = get_width(n = 7), colour = "black", fill = "grey", ) p3 <- diamonds |> ggplot(aes(y = color)) + geom_bar( width = get_width(n = 7, orientation = "y"), colour = "black", fill = "grey", ) p4 <- mpg |> ggplot(aes(x = drv, group = factor(cyl))) + geom_bar( position = position_dodge(preserve = "single"), width = get_width(n = 3, n_dodge = 4), colour = "black", fill = "grey", ) p1 + p2 + p3 + p4 d <- tibble::tibble( continent = c("Europe", "Europe", "Europe", "Europe", "Europe", "South America", "South America"), country = c("AT", "DE", "DK", "ES", "PK", "TW", "BR"), value = c(10L, 15L, 20L, 25L, 17L, 13L, 5L) ) max_n <- d |> count(continent) |> pull(n) |> max() d |> mutate(country = forcats::fct_rev(country)) |> ggplot(aes(y = country, x = value)) + geom_col( width = get_width(n = max_n, orientation = "y"), colour = "black", fill = "grey", ) + facet_wrap(~continent, scales = "free_y") + scale_y_discrete(continuous.limits = c(1, max_n)) + coord_cartesian(reverse = "y", clip = "off") mpg |> ggplot(aes(x = drv)) + geom_bar( width = get_width(n = 3, panel_widths = unit(160, "mm")), colour = "black", fill = "grey", ) + theme(panel.widths = unit(160, "mm"))library(ggplot2) library(dplyr) library(patchwork) set_theme( theme_grey() + theme(panel.widths = rep(unit(75, "mm"), 2)) + theme(panel.heights = rep(unit(50, "mm"), 2)) ) set_equiwidth(1) p1 <- mpg |> ggplot(aes(x = drv)) + geom_bar( width = get_width(n = 3), colour = "black", fill = "grey", ) p2 <- diamonds |> ggplot(aes(x = color)) + geom_bar( width = get_width(n = 7), colour = "black", fill = "grey", ) p3 <- diamonds |> ggplot(aes(y = color)) + geom_bar( width = get_width(n = 7, orientation = "y"), colour = "black", fill = "grey", ) p4 <- mpg |> ggplot(aes(x = drv, group = factor(cyl))) + geom_bar( position = position_dodge(preserve = "single"), width = get_width(n = 3, n_dodge = 4), colour = "black", fill = "grey", ) p1 + p2 + p3 + p4 d <- tibble::tibble( continent = c("Europe", "Europe", "Europe", "Europe", "Europe", "South America", "South America"), country = c("AT", "DE", "DK", "ES", "PK", "TW", "BR"), value = c(10L, 15L, 20L, 25L, 17L, 13L, 5L) ) max_n <- d |> count(continent) |> pull(n) |> max() d |> mutate(country = forcats::fct_rev(country)) |> ggplot(aes(y = country, x = value)) + geom_col( width = get_width(n = max_n, orientation = "y"), colour = "black", fill = "grey", ) + facet_wrap(~continent, scales = "free_y") + scale_y_discrete(continuous.limits = c(1, max_n)) + coord_cartesian(reverse = "y", clip = "off") mpg |> ggplot(aes(x = drv)) + geom_bar( width = get_width(n = 3, panel_widths = unit(160, "mm")), colour = "black", fill = "grey", ) + theme(panel.widths = unit(160, "mm"))
Sets a global default for the equiwidth argument in get_width().
This is the scaling factor that controls the width appearance.
All subsequent calls to get_width() use this value when
equiwidth = NULL, and fall back to 1.
set_equiwidth(equiwidth = 1)set_equiwidth(equiwidth = 1)
equiwidth |
Numeric. Multiplicative factor that controls the width appearance.
A value of |
No return value. Side effects only.
set_equiwidth(1) set_equiwidth(0.75) set_equiwidth(1.33)set_equiwidth(1) set_equiwidth(0.75) set_equiwidth(1.33)