我尝试按照这篇教程(第二部分)在R:https://christophergandrud.github.io/networkD3/中制作网络图
# Load data
library(networkD3)
data(MisLinks)
data(MisNodes)
# Plot
forceNetwork(Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8)
然而,当我在我的数据上尝试同样的方法时,它不起作用(它产生了一个空图像):
Data_I_Have <- data.frame(
"Node_A" = c("John", "John", "John", "Peter", "Peter", "Peter", "Tim", "Kevin", "Adam", "Adam", "Xavier"),
"Node_B" = c("Claude", "Peter", "Tim", "Tim", "Claude", "Henry", "Kevin", "Claude", "Tim", "Henry", "Claude"),
" Place_Where_They_Met" = c("Chicago", "Boston", "Seattle", "Boston", "Paris", "Paris", "Chicago", "London", "Chicago", "London", "Paris"),
"Years_They_Have_Known_Each_Other" = c("10", "10", "1", "5", "2", "8", "7", "10", "3", "3", "5"),
"What_They_Have_In_Common" = c("Sports", "Movies", "Computers", "Computers", "Video Games", "Sports", "Movies", "Computers", "Sports", "Sports", "Video Games")
)
links = data.frame(Data_I_Have$Node_A, Data_I_Have$Node_B)
links$value =1
links$source = Data_I_Have$Node_A
links$target = Data_I_Have$Node_B
links$Node_A = NULL
links$Node_B = NULL
links = links[,c(3,4,5)]
nodes = data.frame( "Node_A" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xavier", "Claude", "Henry"))
nodes$group =1
nodes$name = nodes$Node_A
nodes$Node_A = NULL
# Plot
forceNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8)
有人知道我做错了什么吗?
谢谢。
发布于 2020-11-07 03:33:54
帮助文件显示:
链接:具有节点之间链接的数据框对象。它应该包括每个链接的源和目标。这些应该从0开始编号。可以包括一个可选的值变量来指定节点之间的距离。
您的链接数据框:
str(links)
'data.frame': 11 obs. of 3 variables:
$ value : num 1 1 1 1 1 1 1 1 1 1 ...
$ source: chr "John" "John" "John" "Peter" ...
$ target: chr "Claude" "Peter" "Tim" "Tim" ...
源列和目标列必须是数字
https://stackoverflow.com/questions/64708821
复制相似问题