在 R 中操作 "networkD3" 个对象
Manipulating "networkD3" objects in R
我正在使用 R 中的 networkD3 库为我的数据创建关系图:
library(igraph)
library(dplyr)
library(networkD3)
#create file from which to sample from
x5 <- sample(1:1000000000, 2000, replace=T)
#convert to data frame
x5 = as.data.frame(x5)
#create first file (take a random sample from the created file)
a = sample_n(x5, 1000)
#create second file (take a random sample from the created file)
b = sample_n(x5, 1000)
#combine
c = cbind(a,b)
#create dataframe
c = data.frame(c)
#rename column names
colnames(c) <- c("a","b")
#convert to factors
c$a = as.factor(c$a)
c$b = as.factor(c$b)
#plot graph (with networkD3)
c = data.frame(c)
simpleNetwork(c, fontSize= 20, zoom = T)
当我制作这个图表时,我发现输出不是很容易查看和操作。
我正在尝试使用力图布局,但我在使用它时遇到困难:
forceNetwork(Links = c$b, Nodes = c$a,
Source = "source", Target = "target",
Value = "value", NodeID = "c$a",
opacity = 0.8)
有人可以告诉我我做错了什么吗?
据我了解,forceNetwork
与 simpleNetwork
的数据格式略有不同。
Links 参数需要一个 table 保存存储在节点 table 中的节点的索引(2 列:'from' - 'to',或者如例如 'source' - 'target')。请注意,索引应以 0 而不是 1 开头。
'Links' 的第 3 列应该是边缘宽度。
Nodes 参数需要 table,其中包含 1) 名称和 2) 组。
您在示例中使用的附加参数是您在 'Links' 和 'Nodes' 中使用的 table 中相应列的列名。例如,当您使用 NodeID = "c$a"
时,这意味着您的节点 table 有一个名为 'c$a'.
的列
看看帮助页面:?forceNetwork
他们比我更好地描述了参数,但也向下滚动到示例并检查示例数据的形状(MisLinks 和 MisNodes)。
也许这个小代码可以给你一个方向(继续你的 'c' 对象):
c$a = as.character(c$a)
c$b = as.character(c$b)
Nodes_IDs <- data.frame(name=sort(unique(c(c$a, c$b))))
Nodes_IDs$gr <- 1
# JavaScript need zero-indexed IDs.
c$a <- match(c$a, Nodes_IDs[,1]) -1
c$b <- match(c$b, Nodes_IDs[,1]) -1
c$width <- .5
forceNetwork(Links = c, Nodes = Nodes_IDs,
Source = "a", Target = "b",
Value = "width", NodeID = "name",
Group = "gr",
opacity = 0.8)
我正在使用 R 中的 networkD3 库为我的数据创建关系图:
library(igraph)
library(dplyr)
library(networkD3)
#create file from which to sample from
x5 <- sample(1:1000000000, 2000, replace=T)
#convert to data frame
x5 = as.data.frame(x5)
#create first file (take a random sample from the created file)
a = sample_n(x5, 1000)
#create second file (take a random sample from the created file)
b = sample_n(x5, 1000)
#combine
c = cbind(a,b)
#create dataframe
c = data.frame(c)
#rename column names
colnames(c) <- c("a","b")
#convert to factors
c$a = as.factor(c$a)
c$b = as.factor(c$b)
#plot graph (with networkD3)
c = data.frame(c)
simpleNetwork(c, fontSize= 20, zoom = T)
当我制作这个图表时,我发现输出不是很容易查看和操作。
我正在尝试使用力图布局,但我在使用它时遇到困难:
forceNetwork(Links = c$b, Nodes = c$a,
Source = "source", Target = "target",
Value = "value", NodeID = "c$a",
opacity = 0.8)
有人可以告诉我我做错了什么吗?
据我了解,forceNetwork
与 simpleNetwork
的数据格式略有不同。
Links 参数需要一个 table 保存存储在节点 table 中的节点的索引(2 列:'from' - 'to',或者如例如 'source' - 'target')。请注意,索引应以 0 而不是 1 开头。 'Links' 的第 3 列应该是边缘宽度。
Nodes 参数需要 table,其中包含 1) 名称和 2) 组。
您在示例中使用的附加参数是您在 'Links' 和 'Nodes' 中使用的 table 中相应列的列名。例如,当您使用 NodeID = "c$a"
时,这意味着您的节点 table 有一个名为 'c$a'.
看看帮助页面:?forceNetwork
他们比我更好地描述了参数,但也向下滚动到示例并检查示例数据的形状(MisLinks 和 MisNodes)。
也许这个小代码可以给你一个方向(继续你的 'c' 对象):
c$a = as.character(c$a)
c$b = as.character(c$b)
Nodes_IDs <- data.frame(name=sort(unique(c(c$a, c$b))))
Nodes_IDs$gr <- 1
# JavaScript need zero-indexed IDs.
c$a <- match(c$a, Nodes_IDs[,1]) -1
c$b <- match(c$b, Nodes_IDs[,1]) -1
c$width <- .5
forceNetwork(Links = c, Nodes = Nodes_IDs,
Source = "a", Target = "b",
Value = "width", NodeID = "name",
Group = "gr",
opacity = 0.8)