I have the following example:
library(tidyverse)
library(ggalluvial)
data <- tibble(id = paste0("S", 1:20),
class_1 = c(rep("A", 10), rep("B", 10)),
class_2 = c(rep("A", 8), rep("B", 8), rep("A", 4)))
data_pvt <- data %>%
pivot_longer(starts_with("class"), names_to = "class_type", values_to = "class_label") %>%
mutate(class_type = factor(class_type),
class_label = factor(class_label))
ggplot(data_pvt, aes(x = fct_rev(class_type), stratum = class_label, alluvium = id,
label = class_label)) +
geom_flow(aes(fill = class_label), stat = "alluvium",
lode.guidance = "frontback") +
geom_stratum(aes(fill = class_label)) +
scale_x_discrete(expand = c(0.1, 0)) +
labs(x = "Class system", y = "n") +
coord_flip() +
theme_minimal()
Created on 2022-02-18 by the reprex package (v2.0.1)
I would like geom_flow
to take the fill
color from the top stratum (class_1
) instead of the bottom stratum (class_2
). I could achieve this by not fct_rev(class_type)
at the beginning, but then class_1
is at the bottom, while I want it at the top.
Any ideas? I could use other functions from ggalluvium
or ggforce
, but I'd like to keep the option of having the stratum colored by class_label
.
Are you looking for aes.flow = "backward"
?
ggplot(data_pvt, aes(x = fct_rev(class_type), stratum = class_label, alluvium = id,
label = class_label)) +
geom_flow(aes(fill = class_label), stat = "alluvium",
lode.guidance = "frontback", aes.flow = "backward") +
geom_stratum(aes(fill = class_label)) +
scale_x_discrete(expand = c(0.1, 0)) +
labs(x = "Class system", y = "n") +
coord_flip() +
theme_minimal()