stat_flow | R Documentation |
Given a dataset with alluvial structure, stat_flow
calculates the centroids
(x
and y
) and heights (ymin
and ymax
) of the flows between each pair
of adjacent axes.
stat_flow( mapping = NULL, data = NULL, geom = "flow", position = "identity", decreasing = NULL, reverse = NULL, absolute = NULL, discern = FALSE, negate.strata = NULL, aes.bind = NULL, infer.label = FALSE, min.y = NULL, max.y = NULL, 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 naming the adjustment
(e.g. |
decreasing |
Logical; whether to arrange the strata at each axis in the
order of the variable values ( |
reverse |
Logical; if |
absolute |
Logical; if some cases or strata are negative, whether to
arrange them (respecting |
discern |
Passed to |
negate.strata |
A vector of values of the |
aes.bind |
At what grouping level, if any, to prioritize differentiation
aesthetics when ordering the lodes within each stratum. Defaults to
|
infer.label |
Logical; whether to assign the |
min.y, max.y |
Numeric; bounds on the heights of the strata to be
rendered. Use these bounds to exclude strata outside a certain range, for
example when labeling strata using |
na.rm |
Logical:
if |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Additional arguments passed to |
stat_alluvium
, stat_flow
, and stat_stratum
require one
of two sets of aesthetics:
x
and at least one of alluvium
and stratum
any number of axis[0-9]*
(axis1
, axis2
, etc.)
Use x
, alluvium
, and/or stratum
for data in lodes format
and axis[0-9]*
for data in alluvia format (see alluvial-data
).
Arguments to parameters inconsistent with the format will be ignored.
Additionally, each stat_*()
accepts the following optional
aesthetics:
y
weight
order
group
label
y
controls the heights of the alluvia,
and may be aggregated across equivalent observations.
weight
applies to the computed variables (see that section below)
but does not affect the positional aesthetics.
order
, recognized by stat_alluvium()
and stat_flow()
, is used to
arrange the lodes within each stratum. It tolerates duplicates and takes
precedence over the differentiation aesthetics (when aes.bind
is not
"none"
) and lode guidance with respect to the remaining axes. (It replaces
the deprecated parameter lode.ordering
.)
group
is used internally; arguments are ignored.
label
is used to label the strata or lodes and must take a unique value
across the observations within each stratum or lode.
These and any other aesthetics are aggregated as follows:
Numeric aesthetics, including y
, are summed.
Character and factor aesthetics, including label
,
are assigned to strata or lodes provided they take unique values across the
observations within each (and are otherwise assigned NA
).
These can be used with
ggplot2::after_stat()
to control aesthetic evaluation.
n
number of cases in lode
count
cumulative weight of lode
prop
weighted proportion of lode
stratum
value of variable used to define strata
deposit
order in which (signed) strata are deposited
lode
lode label distilled from alluvia
(stat_alluvium()
and stat_flow()
only)
flow
direction of flow "to"
or "from"
from its axis
(stat_flow()
only)
The numerical variables n
, count
, and prop
are calculated after the
data are grouped by x
and weighted by weight
(in addition to y
).
The integer variable deposit
is used internally to sort the data before
calculating heights. The character variable lode
is obtained from
alluvium
according to distill
.
stat_stratum
, stat_alluvium
, and stat_flow
order strata and lodes
according to the values of several parameters, which must be held fixed
across every layer in an alluvial plot. These package-specific options set
global values for these parameters that will be defaulted to when not
manually set:
ggalluvial.decreasing
(each stat_*
): defaults to NA
.
ggalluvial.reverse
(each stat_*
): defaults to TRUE
.
ggalluvial.absolute
(each stat_*
): defaults to TRUE
.
ggalluvial.cement.alluvia
(stat_alluvium
): defaults to FALSE
.
ggalluvial.lode.guidance
(stat_alluvium
): defaults to "zigzag"
.
ggalluvial.aes.bind
(stat_alluvium
and stat_flow
): defaults to
"none"
.
See base::options()
for how to use options.
The previously defunct parameters weight
and aggregate.wts
have been
discontinued. Use y
and cement.alluvia
instead.
ggplot2::layer()
for additional arguments and
geom_alluvium()
and
geom_flow()
for the corresponding geoms.
Other alluvial stat layers:
stat_alluvium()
,
stat_stratum()
# illustrate positioning ggplot(as.data.frame(Titanic), aes(y = 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", aes(label = after_stat(stratum))) + scale_x_discrete(limits = c("Class", "Sex", "Age")) # alluvium--flow comparison data(vaccinations) gg <- ggplot(vaccinations, aes(x = survey, stratum = response, alluvium = subject, y = freq, fill = response)) + geom_stratum(alpha = .5) + geom_text(aes(label = response), stat = "stratum") # rightward alluvial aesthetics for vaccine survey data gg + geom_flow(stat = "alluvium", lode.guidance = "forward") # memoryless flows for vaccine survey data gg + geom_flow() # size filter examples gg <- ggplot(vaccinations, aes(y = freq, x = survey, stratum = response, alluvium = subject, fill = response, label = response)) + stat_stratum(alpha = .5) + geom_text(stat = "stratum") # omit small flows gg + geom_flow(min.y = 50) # omit large flows gg + geom_flow(max.y = 100) # negate missing entries ggplot(vaccinations, aes(y = freq, x = survey, stratum = response, alluvium = subject, fill = response, label = response, alpha = response != "Missing")) + stat_stratum(negate.strata = "Missing") + geom_flow(negate.strata = "Missing") + geom_text(stat = "stratum", alpha = 1, negate.strata = "Missing") + scale_alpha_discrete(range = c(.2, .6)) + guides(alpha = "none") # 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, y = 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, y = freq, label = response)) + geom_flow(aes(fill = interaction(response, subgroup)), aes.bind = "flows") + scale_alpha_discrete(range = c(1/3, 2/3)) + geom_stratum(alpha = .5) + geom_text(stat = "stratum")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.