0

我有以下示例:

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 包于 2022-02-18 创建(v2.0.1)

我想从顶层 ( ) 而不是底层 ( )geom_flow中获取fill颜色。我可以不在一开始就做到这一点,然后在底部,而我希望它在顶部。class_1class_2fct_rev(class_type)class_1

有任何想法吗?我可以使用ggalluvium或的其他功能ggforce,但我想保留将层着色为 的选项class_label

4

1 回答 1

1

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

在此处输入图像描述

于 2022-02-18T14:41:14.173 回答