使用ggforce创建可跳过节点的Sankey图

时间:2019-09-25 15:34:32

标签: r ggplot2 sankey-diagram ggforce

我想在R中使用ggforce包来创建Sankey图,因为与其他可视化相比,我更喜欢使用ggforce制作的并行集图的外观。我的节点从左到右分为多个级别。但是,我希望某些链接直接从第1级转到第5级,而又不要触及它们之间的节点。此图显示了用networkd3制作的Sankey图,其中“生产”直接链接到中间层的“浪费”。 enter image description here

可以使用ggforce创建这种类型的图吗?我尝试了以下操作,但由于不允许任何级别的缺失值而返回错误。

输入数据

fsc_sankey <- structure(list(stage1 = c("production", "production", "production", 
                                        "production", "production", "production", "production", "production", 
                                        "production"), stage2 = c(NA, "processing", "processing", "processing", 
                                                                  "processing", "processing", "processing", "processing", "processing"
                                        ), stage3 = c(NA, NA, "retail", NA, NA, NA, NA, "retail", "retail"
                                        ), stage4 = c(NA, NA, NA, "foodservice", "foodservice", "institutions", 
                                                      "institutions", "households", "households"), destination = c("waste", 
                                                                                                                   "waste", "waste", "consumed", "waste", "consumed", "waste", "consumed", 
                                                                                                                   "waste"), value = c(1L, 1L, 1L, 3L, 1L, 3L, 1L, 3L, 1L)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                 -9L))

代码

library(tidyverse)
library(ggforce)

fsc_sankey_set <- gather_set_data(fsc_sankey, 1:5) %>%
  mutate(x = factor(x, levels = c('stage1','stage2','stage3','stage4','destination'))) 

ggplot(fsc_sankey_set, aes(x, id = id, split = y, value = value)) +
  geom_parallel_sets(alpha = 0.3, axis.width = 0.1) +
  geom_parallel_sets_axes(axis.width = 0.1) +
  geom_parallel_sets_labels(colour = 'white')

0 个答案:

没有答案