`ggalluvial`: `geom_flow` 从目标层填充颜色

`ggalluvial`: `geom_flow` fill color from destination stratum

我有以下例子:

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

reprex package (v2.0.1)

于 2022-02-18 创建

我想 geom_flow 从顶层 (class_1) 而不是底层 (class_2) 获取 fill 颜色。我可以通过在开始时不使用 fct_rev(class_type) 来实现这一点,但是 class_1 位于底部,而我希望它位于顶部。

有什么想法吗?我可以使用 ggalluviumggforce 中的其他函数,但我想保留使用 class_label.

着色层的选项

您在寻找 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()