Sankey情节与河图包

时间:2017-05-18 12:52:30

标签: r sankey-diagram riverplot

威化。

编辑:解决方案

正如MartineJ和emilliman5所指出的,节点应该是唯一标记的(下图)。

library("riverplot")
nodes<-structure(list(ID = c("2011+", "2011-", "2016+", "2016-"), x = c(20, 
20, 30, 30), y = c(50, 40, 50, 40)), class = "data.frame", row.names = c(NA, 
-4L))
edges<-structure(list(N1 = c("2011+", "2011-", "2011+", "2011-"), N2 = 
c("2016+", "2016-", "2016-", "2016+"), Value = c(461, 7, 0, 46)), class = 
"data.frame", row.names = c(NA, -4L))

river <- makeRiver(nodes,edges)
riverplot(river)

我一直想要绘制一个Sankey图/河图(使用riverplot包)来描述癌症注册随时间的变化,尽管这段代码到目前为止我没那么成功。任何人都可以指导我解释这段代码的错误吗?

Warning message: In checkedges(x2$edges, names(x2)) : duplicated edge information, removing 1 edges

这是可疑代码:

library(“riverplot”)

edges<-structure(list(N1 = c("+", "-", "+", "-"), N2 = c("+", "-", "-", "+"), Value = c(664L, 50L, 0L, 46L)), .Names = c("N1", "N2", "Value"), class = "data.frame", row.names = c(NA, -4L))

nodes = data.frame(ID = unique(c(edges$N1, edges$N2)), stringsAsFactors = FALSE)

nodes$x = c(1,2)
rownames(nodes) = nodes$ID

rp <- list(nodes=nodes, edges=edges)

class(rp) <- c(class(rp), "riverplot")

plot(rp)

数据,包含在代码中:

N1    N2    Value
 +     +     664
 -     -      50
 +     -       0
 -     +      46

永远感激。

2 个答案:

答案 0 :(得分:1)

看起来你在N1(和N2)中多次使用相同的值。尝试使它们完全不同(每列),然后再试一次,f.i。:

N1:plus1减1加2减2

如果你想在makeRiver中显示只有+和 - :,有一个选项** node_labels **

答案 1 :(得分:0)

您的节点需要唯一命名,然后使用nodes$labels将其更改回来:

library(riverplot)

edges<-structure(list(N1 = c("+", "-", "+", "-"), N2 = c("+", "-", "-", "+"), Value = c(664L, 50L, 0L, 46L)), .Names = c("N1", "N2", "Value"), class = "data.frame", row.names = c(NA, -4L))
edges$N1 <- paste0(edges$N1, "a")
edges$N2 <- paste0(edges$N2, "b")
nodes = data.frame(ID = unique(c(edges$N1, edges$N2)), stringsAsFactors = FALSE)

nodes$x = c(1,1,2,2)
nodes$labels <- as.character(substr(nodes$ID, 1, 1))
rownames(nodes) = nodes$ID

rp <- list(nodes=nodes, edges=edges)

class(rp) <- c(class(rp), "riverplot")

plot(rp)

enter image description here