4

我正在使用RsankeyNetwork中的包中的函数,以此处networkD3找到的代码为例。但是,我得到的只是一个空白屏幕。该图应该显示年龄组之间的感染流(按性别)。我的代码如下:

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)
4

4 回答 4

1

您确定您的 R 控制台中没有打印错误吗?

这对我有用,有两个小的修改:

  1. 在开始时也加载 curl 包

    library("curl")
    
  2. fontsize参数显然不起作用,应该删除。

    # Plot
    sankeyNetwork(Links = edges2, Nodes = nodes,
          Source = "source", Target = "target",
          Value = "value", NodeID = "ID",
          width = 700, #fontsize = 12,
          nodeWidth = 30)
    
于 2015-10-27T17:15:17.857 回答
1
  1. 您不需要RCurlread.csv可以直接从 URL 读取
  2. stringsAsFactors = FALSE创建节点 data.frame 时使用该选项可能更安全
  3. 正如其他人指出的那样,您必须确保链接数据中的源变量和目标变量是数字的,并且它们是零索引的
  4. 正如其他人指出的那样,字体大小参数已正确命名fontSize
  5. 我提供了一种更直接的方法来创建带有节点 data.frame 中节点的数字索引的链接数据
library(networkD3)

edges <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/infection_flows.csv",stringsAsFactors = FALSE)

nodes = data.frame(ID = unique(c(edges$Source, edges$Target)), stringsAsFactors = FALSE)

edges$Source <- match(edges$Source, nodes$ID) - 1
edges$Target <- match(edges$Target, nodes$ID) - 1

sankeyNetwork(Links = edges, Nodes = nodes,
              Source = "Source", Target = "Target",
              Value = "Value", NodeID = "ID",
              width = 700, fontSize = 12, nodeWidth = 30)

在此处输入图像描述

于 2019-05-19T10:30:57.390 回答
1

调整字体大小确实有效,但您的论点缺少大写:fontSize

sankeyNetwork(Links = edges2, Nodes = nodes,
  Source = "source", Target = "target",
  Value = "value", NodeID = "ID",
  width = 700, fontSize = 12,
  nodeWidth = 30)
于 2016-01-26T18:09:43.513 回答
0

我通过确保源、目标和值都是数字来为我解决了这个问题。

例如:Energy$links$value <- as.numeric(Energy$links$value)

于 2018-02-21T17:33:58.970 回答