ggalluvial排序层

时间:2018-01-19 17:32:02

标签: r ggplot2

我一直试图用ggalluvial package制作一些sankey图表。我更喜欢它,但我在控制lodes的顺序方面遇到了问题。我正在使用插图开头描述的alluvia格式。

基本上我的图表显示了一个时间点的2级和3级子集以及它们如何移动到另一个时间点。问题是,我不能为我的生活弄清楚如何强制层的顺序,因为图表是不可读的,没有顺序是正确的。这是我的代码:

library("ggalluvial")
library("ggplot2")
subsank_math = structure(list(`Winter Projection` = structure(c(2L, 2L, 2L, 
                                                                2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
                                                                5L, 5L, 5L, 5L, 5L), .Label = c("Level 5", "Level 4", "Level 3", 
                                                                                                "Level 2", "Level 1"), class = "factor"), subgroup = structure(c(1L, 
                                                                                                                                                                 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 
                                                                                                                                                                 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("Level 3 (+)", "Level 3", 
                                                                                                                                                                                                         "Level 3 (-)", "Level 2 (+)", "Level 2", "Level 2 (-)"), class = "factor"), 
                              n = c(119, 102, 16, 10, 12, 1, 272, 544, 182, 151, 134, 22, 
                                    40, 239, 204, 326, 663, 225, 0, 15, 12, 44, 215, 219)), row.names = c(NA, 
                                                                                                          -24L), .Names = c("Winter Projection", "subgroup", "n"), class = "data.frame")




ggplot(subsank_math,
           aes(weight = n,
               axis1 = subgroup, axis2 = `Winter Projection`)) +
geom_alluvium(aes(fill = subgroup),
                  width = 0, knot.pos = 0, reverse = FALSE) +
geom_stratum(width = 1/8, reverse = FALSE) +
geom_text(stat = "stratum", label.strata = TRUE, reverse = FALSE)

有点奇怪,它根据因子的水平而不是第二个轴来排序第一个轴。 Wrong ordering sankey

1 个答案:

答案 0 :(得分:2)

我自己刚刚开始玩ggalluvial软件包,所以我不会声称理解工作原理,但是将数据框格重新格式化为lode格式(在软件包末尾附近描述'小插曲)为我工作:

library(dplyr)
library(tidyr)

df.lode <- subsank_math %>%
  mutate(subject = seq(1, n())) %>%
  gather(x, level, -n, -subject) %>%
  mutate(level = factor(level,
                        levels = c("Level 1", "Level 2 (-)", "Level 2",
                                   "Level 2 (+)", "Level 3 (-)", "Level 3",
                                   "Level 3 (+)", "Level 4")))

> head(df.lode)
    n subject                 x   level
1 119       1 Winter Projection Level 4
2 102       2 Winter Projection Level 4
3  16       3 Winter Projection Level 4
4  10       4 Winter Projection Level 4
5  12       5 Winter Projection Level 4
6   1       6 Winter Projection Level 4

ggplot(df.lode,
       aes(x = x, 
           stratum = level, 
           alluvium = subject,
           weight = n,
           label = level)) +
  geom_flow(aes(fill = level)) +
  geom_stratum() +
  geom_text(stat = "stratum") +
  scale_fill_discrete(limits = levels(df$a1))

plot