Publication-Quality 'ggplot2' Geom Width

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.


ggwidth ggwidth website

CRANstatus

The objective of ggwidth is to standardise ‘ggplot2’ geom width.

It provides methods to ensure the width in ggplot2 geoms are visually consistent across plots with different numbers of categories, panel dimensions, and orientations.

It works with geoms such as geom_bar/geom_col, geom_boxplot and geom_errorbar.

Note this function requires:

  • a set theme with panel widths and heights specified
  • "x" orientation plots to have a x discrete scale with default expand
  • "y" orientation plots to have a y discrete scale with default expand.

Installation

Install from CRAN, or development version from GitHub.

install.packages("ggwidth") 
pak::pak("davidhodge931/ggwidth")

Example

library(ggplot2)
library(dplyr)
library(ggwidth)
library(patchwork)

set_theme(
  ggplot2::theme_grey() +
    theme(panel.widths  = rep(unit(75, "mm"), 2)) +
    theme(panel.heights = rep(unit(50, "mm"), 2))
)

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"))

Other packages

This package is part of a group of related packages built to extend ggplot2.

ggblanket

ggrefine

ggscribe

ggwidth

blends

jumble

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("ggwidth")

0.2.0 by David Hodge, a month ago


https://github.com/davidhodge931/ggwidth, https://davidhodge931.github.io/ggwidth/


Report a bug at https://github.com/davidhodge931/ggwidth/issues


Browse source code at https://github.com/cran/ggwidth


Authors: David Hodge [aut, cre, cph] (ORCID:


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports ggplot2, grid, rlang

Suggests dplyr, forcats, patchwork, testthat, tibble, withr


Suggested by ggscribe.


See at CRAN