r - 使用 d3Network 包绘制网络图时出现空白页面

标签 r

我正在尝试使用 d3Network 包绘制网络图。我尝试组织数据以匹配 package website 上显示的说明。 (和帮助页面),但我仍然得到一个空白网页。谁能发现我做错了什么吗?

library(d3Network)

g.top3000 <- structure(list(from = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 4L, 4L, 5L), .Label = c("afghanistan", "attack", 
"people", "pres_bush", "taliban"), class = "factor"), to = structure(c(4L, 
1L, 5L, 2L, 3L, 1L, 5L, 2L, 3L, 5L, 2L, 3L, 2L, 3L, 3L), .Label = c("people", 
"taliban", "united_states", "attack", "pres_bush"), class = "factor"), 
    weight = c(4, 3, 2, 6, 5, 5, 2, 3, 6, 1, 1, 5, 2, 4, 4)), .Names = c("from", 
"to", "weight"), row.names = c(NA, -15L), class = "data.frame")

top3000.nodes <- structure(list(name = structure(1:5, .Label = c("afghanistan", 
"attack", "people", "pres_bush", "taliban"), class = "factor"), 
    id = c(1, 1, 1, 2, 2)), .Names = c("name", "id"), row.names = c(NA, 
-5L), class = "data.frame")

d3ForceNetwork(Links = g.top3000, Nodes = top3000.nodes, Source = "from", Target = "to",
Value = "weight", NodeID = "name", Group = "id", width = 800, height = 400, opacity = 0.9,
   file = "projekt2_terror_news_force.html")

绘制一个简单的图表效果很好

d3SimpleNetwork(g.top3000, width = 800, height = 400, fontsize = 12, linkDistance = 200,
   file = "projekt2_terror_news.html")

最佳答案

那是因为

  1. 节点列表中没有“联合状态”条目。
  2. 您可能需要使用数字索引而不是节点名称。
<小时/>
# add entry "united status"
top3000.nodes <- rbind(top3000.nodes, data.frame(name = "united_states", id = 3))

# from name to index
g.top3000$from2 <- sapply(as.character(g.top3000$from), function(x) which(x == top3000.nodes$name))-1
g.top3000$to2 <- sapply(as.character(g.top3000$to), function(x) {
    i <- which(x == top3000.nodes$name)
    if (length(i)) i else NA
    }) -1

# use indices in "from2" and "to2"
d3ForceNetwork(Links = g.top3000, Nodes = top3000.nodes, Source = "from2", Target = "to2",
Value = "weight", NodeID = "name", Group = "id", width = 800, height = 400, opacity = 0.4,
   file = "projekt2_terror_news_force.html", linkDistance = 200)

enter image description here

关于r - 使用 d3Network 包绘制网络图时出现空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17701043/

相关文章:

r - testthat:一次期望错误和变量值?

R iGraph : degree in the case of bidirectional edges

r - 读取 https url 时出现 zip 文件错误

r - 根据唯一日期计算记录

r - 针织 latex forloop

R:geom_point() 使用函数来选择形状?

r - 努力模块化我 Shiny 的应用程序

r - 在 R 中,有什么有效的方法可以将两个不同的多层栅格数据聚合/合并为一个吗?

r - DocumentTermMatrix 需要有词频加权误差

r - 如何使用R提取方括号之外的字符串?