Given a dataset with alluvial structure, stat_stratum
calculates the
centroids of the strata at each axis, together with their weights (heights).
stat_stratum(mapping = NULL, data = NULL, geom = "stratum",
position = "identity", decreasing = NA, reverse = TRUE,
discern = FALSE, label.strata = FALSE, min.height = NULL,
max.height = NULL, show.legend = NA, inherit.aes = TRUE,
na.rm = FALSE, ...)
The data to be displayed in this layer. There are three options:
If NULL
, the default, the data is inherited from the plot
data as specified in the call to ggplot()
.
A data.frame
, or other object, will override the plot
data. All objects will be fortified to produce a data frame. See
fortify()
for which variables will be created.
A function
will be called with a single argument,
the plot data. The return value must be a data.frame
, and
will be used as the layer data. A function
can be created
from a formula
(e.g. ~ head(.x, 10)
).
The geometric object to use display the data; override the default.
Position adjustment, either as a string, or the result of a call to a position adjustment function.
Logical; whether to arrange the strata at each axis
in the order of the variable values (NA
, the default),
in ascending order of total weight (largest on top, FALSE
), or
in descending order of total weight (largest on bottom, TRUE
).
Logical; if decreasing
is NA
,
whether to arrange the strata at each axis
in the reverse order of the variable values,
so that they match the order of the values in the legend.
Ignored if decreasing
is not NA
.
Defaults to TRUE
.
Passed to to_lodes_form()
if data
is in
alluvia format.
Logical; whether to assign the values of the axis
variables to the strata. Defaults to FALSE, and requires that no
label
aesthetic is assigned.
Numeric; bounds on the heights (weights) of the
strata to be rendered. Use these bounds to exclude strata outside a certain
range, for example when labeling strata using ggplot2::geom_text()
.
logical. Should this layer be included in the legends?
NA
, the default, includes if any aesthetics are mapped.
FALSE
never includes, and TRUE
always includes.
It can also be a named logical vector to finely select the aesthetics to
display.
If FALSE
, overrides the default aesthetics,
rather than combining with them. This is most useful for helper functions
that define both data and aesthetics and shouldn't inherit behaviour from
the default plot specification, e.g. borders()
.
Logical:
if FALSE
, the default, NA
lodes are not included;
if TRUE
, NA
lodes constitute a separate category,
plotted in grey (regardless of the color scheme).
Additional arguments passed to ggplot2::layer()
.
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_*
layer accepts the following optional
aesthetics:
y
group
y
controls the heights of the alluvia
and may be aggregated across equivalent observations.
group
is used internally; arguments are ignored.
Finally, stat_stratum
accepts the following optional aesthetic:
label
label
is used to label the strata and must take a unique value across
the observations within each stratum.
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 provided they take unique values across the
observations within each stratum (otherwise NA
is assigned).
ggplot2::layer()
for additional arguments and
geom_stratum()
for the corresponding geom.
Other alluvial stat layers: stat_alluvium
,
stat_flow
# NOT RUN {
# only `stratum` assignment is necessary to generate strata
data(vaccinations)
ggplot(vaccinations,
aes(y = freq,
x = survey, stratum = response,
fill = response)) +
stat_stratum(width = .5)
# lode data: positioning with weight labels
ggplot(vaccinations,
aes(y = freq,
x = survey, stratum = response, alluvium = subject,
label = freq)) +
stat_stratum(geom = "errorbar") +
geom_text(stat = "stratum")
# lode data: positioning with stratum labels
ggplot(vaccinations,
aes(y = freq,
x = survey, stratum = response, alluvium = subject,
label = response)) +
stat_stratum(geom = "errorbar") +
geom_text(stat = "stratum")
# alluvium data: positioning with weight labels
ggplot(as.data.frame(Titanic),
aes(y = Freq,
axis1 = Class, axis2 = Sex, axis3 = Age, axis4 = Survived,
label = Freq)) +
geom_text(stat = "stratum") +
stat_stratum(geom = "errorbar") +
scale_x_discrete(limits = c("Class", "Sex", "Age", "Survived"))
# alluvium data: positioning with stratum labels
ggplot(as.data.frame(Titanic),
aes(y = Freq,
axis1 = Class, axis2 = Sex, axis3 = Age, axis4 = Survived)) +
geom_text(stat = "stratum", label.strata = TRUE) +
stat_stratum(geom = "errorbar") +
scale_x_discrete(limits = c("Class", "Sex", "Age", "Survived"))
# omit labels for strata outside a weight range
ggplot(vaccinations,
aes(y = freq,
x = survey, stratum = response,
fill = response, label = response)) +
stat_stratum(width = .5) +
geom_text(stat = "stratum", min.height = 100)
# }
Run the code above in your browser using DataLab