Overview
This article will demonstrate a random assortment of content, including some of which is more advanced.
- Change the
statof the layer - Change the
positionof the layer - Reorder and/or reverse categorical variables
- Drop unused categorical variable values
- Transform scales to
"log"etc - Correct the default orientation
- Avoid the ‘symmetric’ scale
- Change the
*_positionof positional axes - Zoom in or out on scales
- Use delayed evaluation
- Rescale a diverging col scale
- Add a legend within the panel
- Specifying panel sizes
library(dplyr)
library(tidyr)
library(forcats)
library(stringr)
library(ggplot2)
library(scales)
library(ggblanket)
library(patchwork)
library(palmerpenguins)
set_blanket()
penguins2 <- penguins |>
labelled::set_variable_labels(
bill_length_mm = "Bill length (mm)",
bill_depth_mm = "Bill depth (mm)",
flipper_length_mm = "Flipper length (mm)",
body_mass_g = "Body mass (g)",
) |>
mutate(sex = stringr::str_to_sentence(sex)) Change the stat of the layer
The default stat of each gg_* function can
be changed.
penguins2 |>
gg_pointrange(
stat = "summary",
x = species,
y = flipper_length_mm,
)
library(ggforce)
ggplot2::economics |>
slice_head(n = 35) |>
gg_path(
stat = "bspline", n = 100,
x = date,
y = unemploy,
y_label = "Unemployment",
linewidth = 1,
) 
Change the position of the layer
The default position of each gg_* function
can be changed.
penguins2 |>
gg_point(
position = ggbeeswarm::position_quasirandom(),
x = sex,
y = flipper_length_mm,
col = sex,
) +
theme(legend.position = "none")
Reorder and/or reverse categorical variables
ggblanket requires unquoted variables only for x,
y, col, facet,
facet2 and alpha. You can often manipulate the
data prior to plotting to achieve what you want (e.g. using
tidyr::drop_na, forcats::fct_rev and/or
forcats::fct_reorder).
p1 <- diamonds |>
count(color) |>
gg_col(
x = n,
y = color,
width = 0.75,
x_labels = \(x) x / 1000,
x_label = "Count (thousands)",
subtitle = "\nDefault order"
)
p2 <- diamonds |>
count(color) |>
mutate(color = fct_rev(fct_reorder(color, n))) |>
gg_col(
x = n,
y = color,
width = 0.75,
x_labels = \(x) x / 1000,
x_label = "Count (thousands)",
subtitle = "\nRe-orderered"
)
p1 + p2
Drop unused categorical variable values
ggblanket keeps unused factor levels in the plot. If users wish to
drop unused levels they should likewise do it in the data prior to
plotting using forcats::fct_drop.
p1 <- diamonds |>
count(color) |>
filter(color %in% c("E", "G", "I")) |>
gg_col(
x = n,
y = color,
width = 0.75,
x_labels = \(x) x / 1000,
x_label = "Count (thousands)",
subtitle = "\nUnused levels kept",
)
p2 <- diamonds |>
count(color) |>
filter(color %in% c("E", "G", "I")) |>
mutate(color = forcats::fct_drop(color)) |>
gg_col(
x = n,
y = color,
width = 0.75,
x_labels = \(x) x / 1000,
x_label = "Count (thousands)",
subtitle = "\nUnused levels dropped",
)
p1 + p2
Transform scales to "log" etc
Transform objects (e.g. transform_log() or character
strings of these can be used to transform scales - including combining
these.
p1 <- pressure |>
gg_point(
x = temperature,
y = pressure,
x_breaks_n = 4,
y_breaks_n = 4,
subtitle = "\nDefault",
)
p2 <- pressure |>
gg_point(
x = temperature,
y = pressure,
x_breaks_n = 4,
y_breaks_n = 4,
y_transform = "reverse",
subtitle = "\nReverse",
)
p3 <- pressure |>
gg_point(
x = temperature,
y = pressure,
x_breaks_n = 4,
y_breaks_n = 4,
y_transform = "sqrt",
subtitle = "\nSqrt",
)
p4 <- pressure |>
gg_point(
x = temperature,
y = pressure,
x_breaks_n = 4,
y_breaks_n = 4,
y_transform = c("sqrt", "reverse"),
subtitle = "\nSqrt & Reverse",
)
(p1 + p2) / (p3 + p4)
Correct the default orientation
The gg_* function guesses the *_orientation
of the plot to determine how to make continuous axes and what
side-effects to have on the provided theme. If it guesses incorrectly,
use either the x_orientation or y_orientation
argument.
p1 <- penguins2 |>
gg_point(
x = bill_depth_mm,
y = bill_length_mm,
subtitle = "\nDefault theme orientation",
)
p2 <- penguins2 |>
gg_point(
x = bill_depth_mm,
y = bill_length_mm,
theme_orientation = "y",
subtitle = "\nAdjusted theme orientation",
)
p1 + p2
Avoid the ‘symmetric’ scale
Symmetric scales can be turned off or on using
*_symmetric arguments.
p1 <- penguins2 |>
gg_pointrange(
x = sex,
y = bill_length_mm,
stat = "summary",
position = position_dodge(),
x_labels = \(x) str_sub(x, 1, 1),
subtitle = "\ny_symmetric = NULL",
) +
labs(y = NULL)
p2 <- penguins2 |>
gg_pointrange(
x = sex,
y = bill_length_mm,
stat = "summary",
position = position_dodge(),
x_labels = \(x) str_sub(x, 1, 1),
y_symmetric = FALSE,
subtitle = "\ny_symmetric = FALSE,",
) +
labs(y = NULL)
p3 <- penguins2 |>
gg_col(
x = sex,
y = bill_length_mm,
stat = "summary",
position = position_dodge(),
width = 0.5,
x_labels = \(x) str_sub(x, 1, 1),
y_symmetric = FALSE,
subtitle = "\ny_symmetric = FALSE,",
) +
labs(y = NULL)
p1 + p2 + p3
Change the *_position of positional axes
Positional axes can be changed using *_position.
Note that for x_position = "top", a caption must be
added or modified to make this work nicely with a *_mode_*
theme.
economics |>
gg_line(
x = date,
y = unemploy,
col = date,
y_position = "right",
x_position = "top",
caption = "",
title = "Unemployment",
subtitle = "1967\u20132015",
) 
Zoom in or out on scales
There are no *_limits arguments in ggblanket.
Instead, users should use a combination of filtering the data, adding
*_expand_limits and
coord = coord_cartesian(xlim = ..., ylim = ...) arguments
etc.
#To Zoom out, use *_expand_limits:
penguins |>
gg_smooth(
x = body_mass_g,
y = bill_depth_mm,
x_limits_include = c(0),
y_limits_include = c(10, 25),
se = TRUE,
) 
#To zoom-in when the stat equals "identity", use dplyr::filter
penguins |>
filter(bill_depth_mm < 15) |>
gg_point(
x = bill_depth_mm,
y = body_mass_g,
) 
#To zoom-in when the stat does _not_ equal "identity", use coord_cartesian
#Then either recreate the breaks, or turn off the symmetric axis
penguins |>
gg_smooth(
x = body_mass_g,
y = bill_depth_mm,
coord = coord_cartesian(ylim = c(14.8, 15)),
y_breaks = scales::breaks_width(0.05),
se = TRUE,
# y_symmetric = FALSE,
) 
Use delayed evaluation
The mapping argument can be used for delayed evaluation
with the ggplot2::after_stat function.
penguins2 |>
gg_histogram(
x = flipper_length_mm,
mapping = aes(y = after_stat(density)),
facet = species,
)
faithfuld |>
gg_contour(
x = waiting,
y = eruptions,
z = density,
mapping = aes(colour = after_stat(level)),
bins = 8,
)
Rescale a diverging col scale
Use col_rescale to rescale a diverging scale around a
central point.
rescale_vctr <- sort(c(range(mpg$cty), 15))
mpg |>
gg_point(
x = displ,
y = hwy,
col = cty,
col_palette = c(navy, teal, "white", orange, red),
col_rescale = scales::rescale(rescale_vctr),
col_breaks = scales::breaks_width(5),
theme = dark_mode_r(),
)
Add a legend within the panel
set_blanket()
penguins2 |>
gg_histogram(
x = flipper_length_mm,
col = species,
) +
theme(legend.position = "inside") +
theme(legend.position.inside = c(1, 0.975)) +
theme(legend.justification = c(1, 1)) 
Specifying panel sizes
For non-faceted plots, use patchwork::plot_layout.
penguins |>
drop_na(sex) |>
gg_point(
x = flipper_length_mm,
y = body_mass_g,
col = species,
x_breaks_n = 4,
) +
patchwork::plot_layout(
widths = unit(50, "mm"),
heights = unit(50, "mm"),
)
For faceted plots, use purrr::map and
patchwork::wrap_plots.
purrr::map(unique(penguins$species), \(x) {
penguins |>
drop_na(sex) |>
filter(species == x) |>
gg_point(
x = flipper_length_mm,
y = body_mass_g,
col = sex,
position = position_dodge(preserve = "single"),
subtitle = x,
x_breaks_n = 4,
y_breaks_n = 5,
x_limits_include = range(penguins$flipper_length_mm, na.rm = TRUE),
y_limits_include = range(penguins$body_mass_g, na.rm = TRUE),
) +
theme(plot.title.position = "panel") +
theme(plot.subtitle = element_text(hjust = 0.5, margin = margin(b = 11)))
}
) |>
wrap_plots(ncol = 2,
widths = unit(50, "mm"),
heights = unit(50, "mm")) +
guide_area() +
plot_layout(
guides = 'collect',
axes = "collect_x",
)