# NOT RUN {
# 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 = "alluvium") +
stat_alluvium(geom = "pointrange") +
geom_text(stat = "stratum", infer.label = TRUE) +
scale_x_discrete(limits = c("Class", "Sex", "Age"))
# lode ordering examples
gg <- ggplot(as.data.frame(Titanic),
aes(y = Freq,
axis1 = Class, axis2 = Sex, axis3 = Age)) +
geom_stratum() + geom_text(stat = "stratum", infer.label = TRUE) +
scale_x_discrete(limits = c("Class", "Sex", "Age"))
# use of lode controls
gg + geom_flow(aes(fill = Survived, alpha = Sex), stat = "alluvium",
lode.guidance = "forward")
# prioritize aesthetic binding
gg + geom_flow(aes(fill = Survived, alpha = Sex), stat = "alluvium",
aes.bind = TRUE, lode.guidance = "forward")
# use of lode ordering
lode_ord <- replicate(n = 3, expr = sample(x = 32), simplify = FALSE)
print(lode_ord)
gg + geom_flow(aes(fill = Survived, alpha = Sex), stat = "alluvium",
lode.ordering = lode_ord)
# fixed lode ordering across axes
gg + geom_flow(aes(fill = Survived, alpha = Sex), stat = "alluvium",
lode.ordering = lode_ord[[1]])
# use of custom luide guidance function
lode_custom <- function(n, i) {
stopifnot(n == 3)
switch(
i,
`1` = 1:3,
`2` = c(2, 3, 1),
`3` = 3:1
)
}
gg + geom_flow(aes(fill = Survived, alpha = Sex), stat = "alluvium",
aes.bind = TRUE, lode.guidance = lode_custom)
data(majors)
# omit missing elements & reverse the `y` axis
ggplot(majors,
aes(x = semester, stratum = curriculum, alluvium = student, y = 1)) +
geom_alluvium(fill = "darkgrey", na.rm = TRUE) +
geom_stratum(aes(fill = curriculum), color = NA, na.rm = TRUE) +
theme_bw() +
scale_y_reverse()
# alluvium cementation examples
gg <- ggplot(majors,
aes(x = semester, stratum = curriculum, alluvium = student,
fill = curriculum)) +
geom_stratum()
# diagram with outlined alluvia and labels
gg + geom_flow(stat = "alluvium", color = "black") +
geom_text(aes(label = as.integer(student)), stat = "alluvium")
# cemented diagram with default label cementation
gg +
geom_flow(stat = "alluvium", color = "black", cement.alluvia = TRUE) +
geom_text(aes(label = as.integer(student)), stat = "alluvium",
cement.alluvia = TRUE)
# cemented diagram with custom label cementation
gg +
geom_flow(stat = "alluvium", color = "black", cement.alluvia = TRUE) +
geom_text(aes(label = as.integer(student)), stat = "alluvium",
cement.alluvia = function(x) paste(x, collapse = "; "))
# irregular spacing between axes of a continuous variable
data(Refugees, package = "alluvial")
refugees_sub <- subset(Refugees, year %in% c(2003, 2005, 2010, 2013))
ggplot(data = refugees_sub,
aes(x = year, y = refugees, alluvium = country)) +
geom_alluvium(aes(fill = country),
alpha = .75, decreasing = FALSE, knot.pos = 1) +
geom_stratum(aes(stratum = country), decreasing = FALSE, width = 1/2) +
theme_bw() +
scale_fill_brewer(type = "qual", palette = "Set3")
# }
# NOT RUN {
data(babynames, package = "babynames")
# a discontiguous alluvium
bn <- subset(babynames, prop >= .01 & sex == "F" & year > 1962 & year < 1968)
ggplot(data = bn,
aes(x = year, alluvium = name, y = prop)) +
geom_alluvium(aes(fill = name, color = name == "Tammy"),
decreasing = TRUE, show.legend = FALSE) +
scale_color_manual(values = c("#00000000", "#000000"))
# filling in missing zeros
bn2 <- merge(bn,
expand.grid(year = unique(bn$year), name = unique(bn$name)),
all = TRUE)
bn2$prop[is.na(bn2$prop)] <- 0
ggplot(data = bn2,
aes(x = year, alluvium = name, y = prop)) +
geom_alluvium(aes(fill = name, color = name == "Tammy"),
decreasing = TRUE, show.legend = FALSE) +
scale_color_manual(values = c("#00000000", "#000000"))
# }
# NOT RUN {
# use negative y values to encode deaths versus survivals
titanic <- as.data.frame(Titanic)
titanic <- transform(titanic, Lives = Freq * (-1) ^ (Survived == "No"))
ggplot(subset(titanic, Class != "Crew"),
aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Lives)) +
geom_alluvium(aes(alpha = Survived, fill = Class), absolute = FALSE) +
geom_stratum(absolute = FALSE) +
geom_text(stat = "stratum", infer.label = TRUE, absolute = FALSE) +
scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.1, .05)) +
scale_alpha_discrete(range = c(.25, .75), guide = FALSE)
# }
Run the code above in your browser using DataLab