| donut_geom | R Documentation |
Create pie or donut charts while retaining ggplot flexibility, such as leveraging faceting and palettes, and fine-tuning appearance
The function geom_donut_int() creates visually internal donut layer as aggregation of passed values
The function geom_donut_ext() creates visually external donut layer of passed values
geom_donut_int0() and geom_donut_ext() are generic geoms not supporting highlight feature
geom_donut_int0(
mapping = NULL,
data = NULL,
stat = "donut_int",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
r_int = 0,
r_ext = 1,
hl_shift = 0.1,
...
)
geom_donut_int(..., hl_col = "firebrick")
geom_donut_ext0(
mapping = NULL,
data = NULL,
stat = "donut_ext",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
r_int = 1.5,
r_ext = 2,
hl_shift = 0.1,
...
)
geom_donut_ext(..., hl_col = "firebrick")
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this
layer, either as a |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
r_int |
Internal donut radius |
r_ext |
External pie or donut radius |
hl_shift |
Sets the spacing to show highlighted segments |
... |
Other arguments passed on to |
hl_col |
Sets the color for highlighted segments. It's possible to use both simultaneously |
An object of class StatDonutInt (inherits from Stat, ggproto, gg) of length 4.
An object of class StatDonutIntHl (inherits from Stat, ggproto, gg) of length 4.
An object of class StatDonutExt (inherits from Stat, ggproto, gg) of length 4.
An object of class StatDonutExtHl (inherits from Stat, ggproto, gg) of length 4.
There are two additional aesthetics possible to use:
highlight - optional aesthetic which expects logical (TRUE/FALSE) variable in order to highlight particular donut segments
opacity - operates pretty much the same as alpha but ensure more contrast colors and removes legend. Once alpha is set opacity does not affect a chart
None
# Create an example
set.seed(1605)
n <- 20
df <- dplyr::tibble(
lvl1 = sample(LETTERS[1:5], n, TRUE),
lvl2 = sample(LETTERS[6:24], n, TRUE),
value = sample(1:20, n, TRUE),
highlight_ext = sample(c(FALSE,TRUE), n, TRUE, c(.7, .3))) |>
dplyr::mutate(highlight_int = ifelse(lvl1 == "A", TRUE, FALSE))
# Create a simple pie chart
ggplot(df, aes(value = value, fill=lvl1)) +
geom_donut_int(alpha=.6) +
coord_polar(theta = "y")
# Create a simple donut chart that can handle more granular data
# and highlight specific segments
ggplot(df, aes(value = value, fill=lvl2, highlight=highlight_ext)) +
geom_donut_int(r_int=.5, alpha=.6, linewidth=.2) +
coord_polar(theta = "y") +
xlim(0, 1.5)
# Perform data preparation tasks with `packing()`
# and apply specific color
packing(df, value) |>
ggplot(aes(value = value, fill=lvl2, highlight=highlight_ext)) +
geom_donut_int(r_int=.5, alpha=.6, linewidth=.2, col = "gray20") +
coord_polar(theta = "y") +
xlim(0, 1.5)
# Built combined donut chart with interanl and external layers
dplyr::bind_rows(
# arrange by value
`arrange()` = dplyr::arrange(df, lvl1, lvl2, value),
# pack values for better space management
`packing()` = packing(df, value, lvl1),
.id = "prep_type") |>
ggplot(aes(value = value, fill=lvl1)) +
geom_donut_int(aes(highlight=highlight_int), alpha=.6) +
geom_donut_ext(aes(opacity=lvl2, highlight=highlight_int)) +
# apply facets
facet_grid(~prep_type) +
# style chart with palette and theme
scale_fill_viridis_d(option = "inferno", begin = .1, end = .7) +
theme_void() +
coord_polar(theta = "y") +
xlim(0, 2.5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.