
Apply the RO ggplot2 theme and auto-fix common RO style requirements
Source:R/ro_gg_plot.R
ro_gg_plot.Rd
ro_gg_plot() is a concept/draft function that explores applying the RO visual style in a single step, instead
of requiring the separate calls to ro_gg_theme(), ro_gg_remove_axis_gap(), ro_gg_axis(),
a manual thousands-separator, ro_gg_group_ticks(), and ro_gg_y_title() described in
the vignette("ggplot2-in-ro-style") vignette.
Unlike ro_gg_theme(), which returns a theme() object that only carries cosmetic
settings, ro_gg_plot() takes the whole plot as input and returns a modified plot. This
is required because several RO style rules (axis limits/expansion, number formatting, y-axis
title position) depend on the data, the scales, and the geoms already present on the plot,
none of which a plain theme() object can see. Call ro_gg_plot() last, after all layers,
scales, and labels have been added.
Usage
ro_gg_plot(
plot,
flip = FALSE,
base_size = 12,
base_family = "RijksoverheidSansWebText",
lang = c("nl", "en"),
auto_set_y_title = TRUE
)Arguments
- plot
A ggplot object, with all layers, scales, and labels already added.
- flip
Logical. Passed on to
ro_gg_theme(). Default = FALSE.- base_size
Integer. Passed on to
ro_gg_theme(). Default = 12.- base_family
Character. Passed on to
ro_gg_theme(). Default = "RijksoverheidSansWebText".- lang
Character.
"nl"(default) uses a dot as thousands separator (e.g. "1.000");"en"uses a comma (e.g. "1,000").- auto_set_y_title
Logical. If
TRUE(default), relocates the y-axis title viaro_gg_y_title(), as described above. Set toFALSEto leave the y-axis title alone (ro_gg_plot()then only applies the cosmetic theme, legend, and axis changes) so you can position it yourself, e.g. with your ownggplot2::annotation_custom()call, or viatheme(axis.title.y = ...)if the default placement isn't needed at all.
What this does automatically
Applies the
ro_gg_theme()cosmetic theme.Removes legend titles (RO style never shows one), for
fill,colour/color,shape,linetype,alpha, andsize.For bar/column charts (any layer using
geom_col()/geom_bar()), sets the group width to 80% of each category slot, removes any padding between bars within the same group (switchingposition_dodge2()toposition_dodge()if needed), and removes bar outlines. Tick marks are then moved from under each category to the boundary between groups viaro_gg_group_ticks(). Works for both the default and flipped (flip = TRUE) orientation.For bar/column and line charts (any layer using
geom_col()/geom_bar()/geom_line()), and only if the user has not already added a scale for the continuous axis, appliesro_gg_remove_axis_gap()expansion,ro_gg_axis()as the axis limits (so the continuous axis starts at 0), and a thousands separator on the labels. Other chart types (e.g. scatter plots) only get the thousands separator.Relocates the y-axis title to the RO position via
ro_gg_y_title(), when a y-axis title is present. Works for both the default and flipped (flip = TRUE) orientation.
Known limitations
The bar group width (80%), zero padding within a group, and tick-mark styling (color/width/length) are fixed and not user-configurable; if you need different values, apply
ro_gg_group_ticks()and the geom parameters yourself instead ofro_gg_plot().Detecting whether a scale was already set by the user relies on
plot$scales$scales, which is a bit buggy.Facetted plots are not specifically handled;
ro_gg_y_title()andro_gg_group_ticks()only look at the first panel.
See also
ro_gg_theme(),ro_gg_remove_axis_gap(),ro_gg_axis(),ro_gg_group_ticks(), andro_gg_y_title(), whichro_gg_plot()combines.
Other ggplot2:
ro_gg_axis(),
ro_gg_group_ticks(),
ro_gg_remove_axis_gap(),
ro_gg_save(),
ro_gg_theme(),
ro_gg_y_title()
Examples
if (FALSE) { # \dontrun{
library(ggplot2)
dat <- data.frame(
agegroup = c("0-19", "20-39", "40-59"),
n = c(1200, 4800, 6100)
)
p <- ggplot(dat, aes(x = agegroup, y = n)) +
geom_col() +
labs(title = "Cases by age group", y = "Number of cases", x = "Age group")
# build the plot fully with `+` first, then call ro_gg_plot() on the finished object
ro_gg_plot(p)
} # }