Skip to content

Instantly share code, notes, and snippets.

@kjhealy
Last active April 17, 2024 11:39
Show Gist options
  • Save kjhealy/76f68f4264ad1763511747f959bf973b to your computer and use it in GitHub Desktop.
Save kjhealy/76f68f4264ad1763511747f959bf973b to your computer and use it in GitHub Desktop.
### Messing around with alluvial plots
library(tidyverse)
library(here)
### -------------- Note on Fonts --------------------
## For these fonts to work you will need to have
## the full Myriad Pro font installed along with
## my {myriad} package. Myriad Pro is available
## from Adobe, and may already be installed on you
## computer. The {myriad} package is here:
## https://kjhealy.github.io/myriad/
## Install it with:
## remotes::install_github("kjhealy/myriad")
## If you don't have the fonts, you can comment
## out everything from here until the "Font setup ends"
## line below. Also change or remove the references
## to "Myriad Pro Condensed" in the main code below.
## With those removed, the plots will still work, but
## they'll be in Helvetica, and they won't look as good.
library(systemfonts)
clear_registry()
register_variant(
name = "Myriad Pro SemiCondensed",
family = "Myriad Pro",
width = "semicondensed",
weight = c("normal", "semibold"),
)
library(showtext)
showtext_opts(dpi = 300)
showtext_auto()
library(myriad)
import_myriad_semi()
import_myriad_condensed()
theme_set(theme_myriad_semi())
### -------------- Font Setup Ends --------------------
### Begin figures proper
## For the flow diagrams
# install.packages("ggalluvial")
library(ggalluvial)
## For the inside labels
# install.packages("ggfittext")
library(ggfittext)
## Built in to the package
data(majors)
head(majors)
## Convert df to tibble and make the labels a little nicer.
## Also there's a typo in the sample data for some reason.
majors_df <- as_tibble(majors) |>
mutate(
curriculum = str_replace(curriculum, "Sculpure", "Sculpture"),
curriculum_f = fct_na_value_to_level(as.factor(curriculum),
"(Missing)"),
semester_f = str_replace(semester, "CURR", ""),
semester_f= str_pad(semester_f, width = 2, pad = "0")) # So it'll sort properly
###
### Make the plot
###
p1 <- majors_df |>
ggplot(aes(x = semester_f,
stratum = curriculum_f,
alluvium = student, # the thing that "flows"
fill = curriculum_f,
label = curriculum_f)) +
scale_fill_brewer(type = "qual",
palette = "Set2") +
geom_flow(stat = "alluvium",
lode.guidance = "frontback",
color = "gray30") +
geom_stratum() +
theme(legend.position = "bottom") +
labs(title = "Student Curriculums Across Several Semesters",
x = "Semester",
fill = "Curriculum")
p1
## Alluvial/Sankey plots like to be wide
ggsave(p1, file = "alluvial_basic.pdf", height = 5, width = 12)
###
### Experimenting a bit more
###
## Better color palette
library(prismatic)
my_colors <- color(ggokabeito::palette_okabe_ito()[c(1:3, 5:8)])
my_colors_li <- clr_lighten(my_colors)
## Mess with widths
width_factor <- 0.5
p2 <- majors_df |>
mutate(curriculum = fct_na_value_to_level(curriculum, "(Missing)")) |>
ggplot(aes(x = semester_f,
stratum = curriculum_f,
alluvium = student, # the thing that "flows:
fill = curriculum_f,
label = curriculum)) + ##
geom_flow(width = width_factor,
stat = "alluvium",
lode.guidance = "frontback",
color = "gray90",
size = rel(0.2),
alpha = 0.5) +
geom_stratum(width = width_factor,
alpha = 0.8,
color = "gray90",
linewidth = 0.2) +
geom_fit_text(stat = "stratum",
width = width_factor,
family = "Myriad Pro Condensed",
color = "gray99",
min.size = 1) +
scale_fill_manual(values = my_colors) +
scale_x_discrete(expand = c(.1, 0)) +
guides(x = guide_axis(cap = "both")) +
theme(legend.position = "none",
axis.line.x = element_line(color = "gray30",
linewidth = 0.75),
axis.line.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(hjust = 0.5)) +
labs(title = "Student Curriculums Across Several Semesters",
x = "Semester")
p2
ggsave(p2, file = "alluvial_polished.pdf", height = 5, width = 12)
ggsave(p2, file = "alluvial_polished.png", height = 5, width = 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment