带有 riverplot 包的桑基图
Sankey plot with the riverplot package
附魔。
编辑:解决方案
正如 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 diagram/riverplot(使用 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
感激不尽。
您似乎在 N1(和 N2)中多次使用相同的值。尝试让它们都不同(每列),然后再试一次,f.i.:
N1: plus1 minus1 plus2 minus2
如果您只想显示 + 和-: 在makeRiver 中,有一个选项**node_labels **
您的节点需要唯一命名,然后使用 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)
附魔。
编辑:解决方案
正如 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 diagram/riverplot(使用 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
感激不尽。
您似乎在 N1(和 N2)中多次使用相同的值。尝试让它们都不同(每列),然后再试一次,f.i.:
N1: plus1 minus1 plus2 minus2
如果您只想显示 + 和-: 在makeRiver 中,有一个选项**node_labels **
您的节点需要唯一命名,然后使用 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)