桑基图

Sankey Diagrams

我看过这个 post 1 and Sankey plot in R 来制作 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"。

让我们试试:

# 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")



结果在这里。