Sankey Diagrams

时间:2018-05-24 16:35:34

标签: r visualization flowchart sankey-diagram

我已经看过这篇文章1Sankey plot in R来制作一个SAnkey图,但理解起来很复杂!

如果有人能够解释如何为下面的数据框制作一个Sankey图;

data.frame(row.names = paste0("SP","",1:30),
           COL1 = rep(sample(LETTERS[1:3]),10),
           COL3 = rep(sample(LETTERS[1:3]),10),
           COL3 = rep(sample(LETTERS[1:3]),10)
           )

因此,我可以将3个条形图的值可视化,其中3个代表3个因子的分段" A"," B"和" C"。

1 个答案:

答案 0 :(得分:1)

让我们试试:

# load some packages!
library(alluvial)
library(ggalluvial)
require(ggplot2)
library(reshape)

# put the seed, to have consistency in random sample
set.seed(1)
data <- data.frame(row.names = paste0("SP","",1:30),
           id = paste0("SP","",1:30),              # added the rowlabels as id
           COL1 = rep(sample(LETTERS[1:3]),10),
           COL2 = rep(sample(LETTERS[1:3]),10),    # rename a column
           COL3 =rep(sample(LETTERS[1:3]),10))


# put data in long format, for ggplot
mdata <- melt(data, id=c("id")) 


# here the sankey, if it's like you want it
ggplot(mdata,aes(x = variable, stratum = value, alluvium = id, fill = value, label = value))+
  geom_flow(stat = "alluvium", lode.guidance = "rightleft") +
  geom_stratum() + geom_text(stat = "stratum")+
  theme(legend.position = "none") +
  ggtitle("test")



这是结果。

enter image description here