我想用library(alluvial)做一个冲积图
我的数据看起来是这样的:
> id Diagnose 1 Diagnose 2 Diagnose 3
1 Cancer cancer cancer
2 Headache Breastcancer Breastcancer
3 Breastcancer Breastcancer cancer
4 Cancer cancer cancer
5 Cancer Breastcancer Breastcancer
6 Cancer Breastcancer cancer 等。
dataframe显示医生诊断的名称(只是示例,而不是真正的诊断)。
所以对于病人id 1,第一种诊断是癌症,第二种是癌症,最后一种是癌症。对于第二个病人,第一个诊断是头痛,然后是诊断乳腺癌等等。
我想做一个冲积图,显示每个病人的诊断进展。并收集所有“癌症”患者作为第一次诊断,等等。我如何制作一个冲积层图,如下所示:
发布于 2019-01-04 10:17:47
您应该首先处理数据,然后使用alluvial函数:
library(dplyr) # to manipulate data
library(alluvial)
allu <- data %>%
group_by(Diagnose1, Diagnose2, Diagnose3) %>% # grouping
summarise(Freq = n()) # adding frequencies
# here the plot
alluvial(allu[,1:3], freq=allu$Freq)

使用数据(我删除了列名中的空格):
data <- read.table(text = "id Diagnose1 Diagnose2 Diagnose3
1 Cancer cancer cancer
2 Headache Breastcancer Breastcancer
3 Breastcancer Breastcancer cancer
4 Cancer cancer cancer
5 Cancer Breastcancer Breastcancer
6 Cancer Breastcancer cancer ",header = T)编辑
如果您有NAs,您可以尝试以这种方式替换它们:
# first, you should use the option stringsAsFactor = F in the data, in my case
data <- read.table(text = "id Diagnose1 Diagnose2 Diagnose3
1 Cancer cancer cancer
2 Headache Breastcancer Breastcancer
3 Breastcancer Breastcancer cancer
4 Cancer NA cancer
5 Cancer Breastcancer Breastcancer
6 Cancer Breastcancer cancer ",header = T, stringsAsFactor = F )
# second, replace them with something you like:
data[is.na(data)] <- 'nothing'最后,你可以画出这个情节,它将出现“选择”这个词来代替NAs。
https://stackoverflow.com/questions/54036847
复制相似问题