R 调试网络3图

R 调试网络3图,r,graph,data-visualization,nodes,networkd3,R,Graph,Data Visualization,Nodes,Networkd3,我在这里尝试了以下教程(第二部分),介绍如何在R中绘制网络图: 然而,当我在我的数据上尝试相同的方法时,它不起作用(它会生成一个空图像): Data\u I\u Have帮助文件显示: 链接:节点之间具有链接的数据帧对象。它应该包括每个链接的源和目标。这些应该从0开始编号。可以包含一个可选的值变量来指定节点之间的距离 您的链接数据框: str(links) 'data.frame': 11 obs. of 3 variables: $ value : num 1 1 1 1 1 1

我在这里尝试了以下教程(第二部分),介绍如何在R中绘制网络图:

然而,当我在我的数据上尝试相同的方法时,它不起作用(它会生成一个空图像):

Data\u I\u Have帮助文件显示:

链接:节点之间具有链接的数据帧对象。它应该包括每个链接的源和目标。这些应该从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" ...
源列和目标列必须是数字

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)
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" ...