我使用R中sankeyNetwork
包中的networkD3
函数作为示例找到的代码here。但是,我得到的只是一个空白屏幕。该图应该显示年龄组之间的感染流量(按性别)。我的代码如下:
library(RCurl)
library(networkD3)
edges <- read.csv(curl("https://raw.githubusercontent.com/kilimba/data/master/infection_flows.csv"),stringsAsFactors = FALSE )
nodes = data.frame(ID = unique(c(edges$Source, edges$Target)))
nodes$indx =0
for (i in 1:nrow(nodes)){
nodes[i,]["indx"] = i - 1
}
edges2 <- merge(edges,nodes,by.x = "Source",by.y = "ID")
edges2$Source <-NULL
names(edges2) <- c("target","value","source")
edges2 <- merge(edges2,nodes,by.x = "target",by.y = "ID")
edges2$target <- NULL
names(edges2) <- c("value","source","target")
nodes$indx <- NULL
# Plot
sankeyNetwork(Links = edges2, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "ID",
width = 700, fontsize = 12, nodeWidth = 30)
答案 0 :(得分:1)
您确定R控制台上没有打印错误吗?
这对我有两个小的改动:
在开头
加载卷曲包library("curl")
fontsize
参数显然不起作用,应该删除。
# Plot
sankeyNetwork(Links = edges2, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "ID",
width = 700, #fontsize = 12,
nodeWidth = 30)
答案 1 :(得分:1)
调整fontsize确实有效,但你的参数缺少大小写:fontSize
sankeyNetwork(Links = edges2, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "ID",
width = 700, fontSize = 12,
nodeWidth = 30)
答案 2 :(得分:0)
我通过确保源,目标和值都是数字来解决这个问题。
例如: Energy $链接$ value&lt; - as.numeric(Energy $ links $ value)
答案 3 :(得分:0)