| pins {donutsk} | R Documentation |
Connecting labels with donut segments
Description
The set of functions served to connect text or labels with donut segments
-
geom_pin_line()- builds curved line to linl label with donut segment -
geom_pin_head()- builds stylish point heads for pins -
geom_pin()- handy wrapper forgeom_pin_line()andgeom_pin_head()
Usage
geom_pin_line(
mapping = NULL,
data = NULL,
stat = "pin",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
r = 1.5,
cut = 0.1,
layout = circle(),
...
)
geom_pin_head(
mapping = NULL,
data = NULL,
stat = "point",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
r = 1.5,
cut = 0.1,
layout = circle(),
...
)
geom_pin(..., head = TRUE)
Arguments
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 |
The radius where donut is placed |
cut |
Sets additional two-sided gap for pins |
layout |
The layout function to effectively display text and labels.
Obviously it's better to have the same as for |
... |
Parameters to be passed to |
head |
Boolean - defines whether to add pin head |
Format
An object of class StatPinLine (inherits from Stat, ggproto, gg) of length 3.
An object of class StatPinHead (inherits from Stat, ggproto, gg) of length 3.
Value
None
See Also
Examples
n <- 30
set.seed(2021)
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(.9, .1))) |>
dplyr::mutate(highlight_int = dplyr::if_else(lvl1 == "A", TRUE, FALSE))
# Starting plot with doubled donuts and annotations for internal one
p <- dplyr::group_by(df, lvl1, lvl2, highlight_ext, highlight_int) |>
dplyr::summarise(value = sum(value), .groups = "drop") |>
packing(value, lvl1) |>
ggplot(aes(value = value, fill = lvl1)) +
geom_donut_int(aes(highlight = highlight_int), alpha=.5, r_int = .25) +
geom_label_int(aes(label = "Sum {fill}:\n{.sum}-{scales::percent(.prc)}"),
alpha = .6, col = "white", size = 3, r=1.2) +
geom_donut_ext(aes(opacity = lvl2, highlight = highlight_ext)) +
scale_fill_viridis_d(option = "inferno", begin = .1, end = .7) +
guides(alpha = guide_legend(ncol = 2), fill = guide_legend(ncol = 2)) +
theme_void() +
theme(legend.position = "none")
p + coord_radial(theta = "y", expand = FALSE, rotate_angle = FALSE)
# Add labels to external donut as percent inside group
p + coord_radial(theta = "y", expand = FALSE, rotate_angle = FALSE) +
geom_label_ext(aes(label = paste0(lvl2, ": {scales::percent(.prc_grp)}")),
show.legend = FALSE, size=3, col="white") +
geom_pin(size = .5, linewidth=.1, show.legend = FALSE, cut = .2)
# Leverage tv() layout
p + coord_radial(theta = "y", expand = FALSE, rotate_angle = FALSE) +
geom_label_ext(aes(label = paste0(lvl2, ":{scales::percent(.prc_grp)}")),
show.legend = FALSE, size=3, col="white",
layout = tv(thinner = TRUE, thinner_gap = .15)) +
geom_pin(size = .5, linewidth=.1, show.legend = FALSE, cut = .2,
layout = tv(thinner = TRUE, thinner_gap = .15))
# Leverage another layout
p + coord_radial(theta = "y", expand = FALSE, rotate_angle = FALSE) +
geom_label_ext(aes(label = paste0(lvl2, ": {scales::percent(.prc_grp)}")),
show.legend = FALSE, size=3, col="white", layout = eye()) +
geom_pin(size = .5, linewidth=.1, show.legend = FALSE, layout = eye())