| get_width | R Documentation |
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
)
... |
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.
set_equiwidth()
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"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.