如何将整齐的数据集转换为冲积友好格式? (冲积)

时间:2019-04-01 11:01:21

标签: r ggplot2 dplyr

我有一个问题,无法以合适的格式获取数据以制成具有冲积层的冲积层图(每个冲积层一行)。我想绘制2018年至2019年不同课程之间的流量。

某些dplyr魔法有可能吗? :)

library(tidyverse)
library(ggalluvial)

id <- as.character(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))

year <- as.character(c(2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019))

class <- c("A", "A", "B", "C", "B", "A", "A", "C", "B", "A", "B", "A", "B", "C", "B", "B", "C", "C", "B", "A")

df <- data.frame(id, year, class)

1 个答案:

答案 0 :(得分:2)

似乎您不需要处理数据,例如:

library(ggplot2)
library(ggalluvial)
df$class <- as.factor(df$class) # only put this as factor
ggplot(df,aes(x = year, stratum = class, alluvium = id, fill = class, label = class)) +
       scale_fill_brewer(type = "qual", palette = "Set2") +
       geom_flow(stat = "alluvium", lode.guidance = "rightleft",color = "darkgray") +
       geom_stratum() + 
       theme_light()

enter image description here