Learn R Programming

ggalluvial (version 0.6.0)

stat_flow: Flow positions

Description

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.

Usage

stat_flow(mapping = NULL, data = NULL, geom = "flow",
  position = "identity", decreasing = NA, reverse = TRUE,
  discern = FALSE, aes.bind = FALSE, na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE, ...)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

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.

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 (NA, the default), in ascending order of total weight (largest on top, FALSE), or in descending order of total weight (largest on bottom, TRUE).

reverse

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.

discern

Passed to to_lodes if data is in alluvia format.

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 FALSE, the default, NA lodes are not included; if TRUE, NA lodes constitute a separate category, plotted in grey (regardless of the color scheme).

show.legend

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.

inherit.aes

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().

...

Additional arguments passed to layer.

Aesthetics

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 is_alluvial). Arguments to parameters inconsistent with the format will be ignored. Additionally, each stat_* layer 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. 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 weight, 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).

See Also

layer for additional arguments and geom_alluvium and geom_flow for the corresponding geoms.

Other alluvial stat layers: stat_alluvium, stat_stratum

Examples

Run this code
# NOT RUN {
# 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)
gg <- ggplot(vaccinations,
             aes(x = survey, stratum = response, alluvium = subject,
                 weight = 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 = "rightward")
# memoryless flows for vaccine survey data
gg + geom_flow()

# 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")

data(majors)
# omit missing lodes and incident flows
ggplot(majors,
       aes(x = semester, stratum = curriculum, alluvium = student)) +
  geom_flow(fill = "darkgrey", na.rm = TRUE) +
  geom_stratum(aes(fill = curriculum), color = NA, na.rm = TRUE) +
  theme_bw()
# }

Run the code above in your browser using DataLab