R中的sankey图-数据准备

时间:2019-06-05 12:49:05

标签: r sankey-diagram networkd3

我有以下数据框,其中每个病人都是一行(我只显示了其中的一个样本):

df = structure(list(firstY = c("N/A", "1", "3a", "3a", "3b", "1", 
"2", "1", "5", "3b"), secondY = c("N/A", "1", "2", "3a", "4", 
"1", "N/A", "1", "5", "3b"), ThirdY = c("N/A", "1", "N/A", "3b", 
"4", "1", "N/A", "1", "N/A", "3b"), FourthY = c("N/A", "1", "N/A", 
"3a", "4", "1", "N/A", "1", "N/A", "3a"), FifthY = c("N/A", "1", 
"N/A", "2", "5", "1", "N/A", "N/A", "N/A", "3b")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -10L))

我想绘制一个Sankey图,该图显示每个患者随时间的轨迹,我知道我必须创建节点和链接,但是在将数据转换为完成此操作所需的格式时遇到了问题。具体来说,最有问题的问题是计算每个轨迹有多少患者,例如,从第1阶段到第2阶段的第一年去了多少患者,以及所有其他组合。

任何有关数据准备的帮助将不胜感激。

Alluvial软件包虽然简单易懂,但是在有大量数据的情况下不能很好地应对。

3 个答案:

答案 0 :(得分:3)

尚不清楚要实现什么目标,因为您没有提及要使用的软件包,但是查看数据,如果可以使用{{1 }}程序包:

alluvial

library(alluvial) # sankey plots library(dplyr) # data manipulation 函数可以像您一样使用宽格式的数据,但是它需要一个频数列,因此我们可以创建它,然后进行绘制:

alluvial

enter image description here

反之,如果您想使用特定的程序包,则应指定哪个程序包。


编辑

使用network3D有点棘手,但是您也许可以从中获得一些不错的结果。您需要链接和节点,并使它们匹配,所以首先我们可以创建链接:

dats_all <- df %>%                                                   # data
            group_by( firstY, secondY, ThirdY, FourthY, FifthY) %>%  # group them
            summarise(Freq = n())                                    # add frequencies

 # now plot it
alluvial( dats_all[,1:5], freq=dats_all$Freq, border=NA )

现在,节点以# put your df in two columns, and preserve the ordering in many levels (columns) with paste0 links <- data.frame(source = c(paste0(df$firstY,'_1'),paste0(df$secondY,'_2'),paste0(df$ThirdY,'_3'),paste0(df$FourthY,'_4')), target = c(paste0(df$secondY,'_2'),paste0(df$ThirdY,'_3'),paste0(df$FourthY,'_4'),paste0(df$FifthY,'_5'))) # now convert as character links$source <- as.character(links$source) links$target<- as.character(links$target) 的方式成为链接中的每个元素:

unique()

现在,我们需要每个节点都有一个链接(反之亦然),因此我们将它们匹配并进行数字转换。请注意末尾的-1,因为networkD3是0索引,这意味着数字(索引)从0开始。

nodes <- data.frame(name = unique(c(links$source, links$target)))

现在,您应该准备绘制Sankey了:

links$source <- match(links$source, nodes$name) - 1
links$target <- match(links$target, nodes$name) - 1
links$value <- 1 # add also a value

enter image description here

答案 1 :(得分:2)

使用 ggforce

library(ggforce)
library(dplyr)

xx <- df %>% 
  count(firstY, secondY, ThirdY, FourthY, FifthY, name = "value") %>% 
  gather_set_data(1:5) %>% 
  mutate(x = factor(x, levels = colnames(df)))


ggplot(xx, 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.3) +
  geom_parallel_sets_labels(colour = "white")

enter image description here

答案 2 :(得分:2)

使用方式

library(tidyr)
library(dplyr)
library(networkD3)

links <-
  df %>% 
  mutate(row = row_number()) %>%  # add a row id
  gather('col', 'source', -row) %>%  # gather all columns
  mutate(col = match(col, names(df))) %>%  # convert col names to col nums
  mutate(source = paste0(source, '_', col)) %>%  # add col num to node names
  group_by(row) %>%
  arrange(col) %>%
  mutate(target = lead(source)) %>%  # get target from following node in row
  ungroup() %>% 
  filter(!is.na(target)) %>%  # remove links from last column in original data
  select(source, target) %>% 
  group_by(source, target) %>% 
  summarise(value = n())  # aggregate and count similar links

# create nodes data frame from unque nodes found in links data frame
nodes <- data.frame(id = unique(c(links$source, links$target)),
                    stringsAsFactors = FALSE)
# remove column id from names
nodes$name <- sub('_[0-9]*$', '', nodes$id)

# set links data to the 0-based index of the nodes in the nodes data frame
links$source <- match(links$source, nodes$id) - 1
links$target <- match(links$target, nodes$id) - 1

sankeyNetwork(Links = links, Nodes = nodes, Source = 'source',
              Target = 'target', Value = 'value', NodeID = 'name')

enter image description here