Given a dataset with alluvial structure, stat_flow
calculates the
centroids (x
and y
) and weights (heights; ymin
and
ymax
) of alluvial flows between each pair of adjacent axes.
stat_flow(mapping = NULL, data = NULL, geom = "flow", position = "identity", decreasing = NA, reverse = TRUE, aes.bind = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...)
mapping | Set of aesthetic mappings created by |
---|---|
data | The data to be displayed in this layer. There are three options: If A A |
geom | The geometric object to use display the data; override the default. |
position | Position adjustment, either as a string, or the result of a call to a position adjustment function. |
decreasing | Logical; whether to arrange the strata at each axis
in the order of the variable values ( |
reverse | Logical; if |
aes.bind | Whether to prioritize aesthetics before axes (other than the index axis) when ordering the lodes within each stratum. Defaults to FALSE. |
na.rm | Logical:
if |
show.legend | logical. Should this layer be included in the legends?
|
inherit.aes | If |
... | Additional arguments passed to |
stat_flow
requires one of two sets of aesthetics:
x
, alluvium
, and (optionally) stratum
any number of axis[0-9]*
(axis1
, axis2
, etc.)
Use x
, alluvium
, and stratum
for data in lodes format
and axis[0-9]*
for data in alluvia format
(see is_alluvial
).
Arguments to parameters inconsistent with the format will be ignored.
Additionally, stat_flow
accepts the following optional aesthetics:
weight
group
weight
controls the vertical dimensions of the alluvia
and are aggregated across equivalent observations.
group
is used internally; arguments are ignored.
layer
for additional arguments,
geom_flow
for the corresponding geom, and
stat_stratum
and geom_stratum
for
intra-axis boxes.
# illustrate positioning ggplot(as.data.frame(Titanic), aes(weight = Freq, axis1 = Class, axis2 = Sex, axis3 = Age, color = Survived)) + stat_stratum(geom = "errorbar") + geom_line(stat = "flow") + stat_flow(geom = "pointrange") + geom_text(stat = "stratum", label.strata = TRUE) + scale_x_continuous(breaks = 1:3, labels = c("Class", "Sex", "Age"))# use of lode controls ggplot(as.data.frame(Titanic), aes(weight = Freq, axis1 = Class, axis2 = Sex, axis3 = Age)) + geom_flow(aes(fill = Survived), aes.bind = TRUE, reverse = FALSE) + geom_stratum(reverse = FALSE) + geom_text(stat = "stratum", label.strata = TRUE, reverse = FALSE) + scale_x_continuous(breaks = 1:3, labels = c("Class", "Sex", "Age"))data(vaccinations) # rightward alluvial aesthetics for vaccine survey data ggplot(vaccinations, aes(x = survey, stratum = response, alluvium = subject, weight = freq, fill = response)) + geom_flow(stat = "alluvium", lode.guidance = "rightward") + geom_stratum(alpha = .5) + geom_text(aes(label = response), stat = "stratum")# memoryless flows for vaccine survey data ggplot(vaccinations, aes(x = survey, stratum = response, alluvium = subject, weight = freq, fill = response)) + geom_flow() + geom_stratum(alpha = .5) + geom_text(aes(label = response), stat = "stratum")# aesthetics that vary betwween and within strata data(vaccinations) vaccinations$subgroup <- LETTERS[1:2][rbinom( n = length(unique(vaccinations$subject)), size = 1, prob = .5 ) + 1][vaccinations$subject] ggplot(vaccinations, aes(x = survey, stratum = response, alluvium = subject, weight = freq, fill = response, label = response)) + geom_flow(aes(alpha = subgroup)) + scale_alpha_discrete(range = c(1/3, 2/3)) + geom_stratum(alpha = .5) + geom_text(stat = "stratum")# can even set aesthetics that vary both ways ggplot(vaccinations, aes(x = survey, stratum = response, alluvium = subject, weight = freq, label = response)) + geom_flow(aes(fill = interaction(response, subgroup)), aes.bind = TRUE) + scale_alpha_discrete(range = c(1/3, 2/3)) + geom_stratum(alpha = .5) + geom_text(stat = "stratum")