R:如何使网络3对数据的子集作出反应
我正在使用R:如何使网络3对数据的子集作出反应,r,htmlwidgets,networkd3,R,Htmlwidgets,Networkd3,我正在使用networkD3软件包,无法使用我的数据创建一些子网络 我有一个这样制作的数据集(MisNodes和MisLinks由包本身提供): 整个网络如下所示: forceNetwork( Links = MisLinks, Nodes = MisNodes, Source = "source", Target = "target", Value = "value", NodeID = "name", Group = "group", opacity = 1
networkD3
软件包,无法使用我的数据创建一些子网络
我有一个这样制作的数据集(MisNodes
和MisLinks
由包本身提供):
整个网络如下所示:
forceNetwork(
Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 1)
现在,我想选择一个组,只查看该组的节点,以及作为该组中节点源的链接:
所以我试了一下:
# here the group
k <-c(1)
# add id, to subset well
MisNodes$id <- rownames(MisNodes)
# select the desired nodes
nodes <- (MisNodes[MisNodes$group %in% k,])
# select the links that have as source the desired nodes
links <- (MisLinks[rownames(MisLinks) %in% nodes$id,])
# rownames from 0
rownames(nodes) <- 1:nrow(nodes)-1
# indexing from 0 to max
links$source_ <-match(links$source, sort(unique(links$source)))-1
links$target_ <-match(links$target, sort(unique(links$target)))-1
似乎源从1开始,目标从0开始,但删除-1后,它对第一个组有效,但对其他组无效。另外,使用
k的总节点ID使用基于0的索引,并且您还应该删除具有不在组中的目标节点的链接,否则您可能具有指向节点数据中不存在的目标节点的链接(在组1中不存在,但在其他组中存在)
库(networkD3)
数据(“错误节点”)
数据(“错误链接”)
不幸的是,我只能给你一个+1,非常感谢。
# here the group
k <-c(1)
# add id, to subset well
MisNodes$id <- rownames(MisNodes)
# select the desired nodes
nodes <- (MisNodes[MisNodes$group %in% k,])
# select the links that have as source the desired nodes
links <- (MisLinks[rownames(MisLinks) %in% nodes$id,])
# rownames from 0
rownames(nodes) <- 1:nrow(nodes)-1
# indexing from 0 to max
links$source_ <-match(links$source, sort(unique(links$source)))-1
links$target_ <-match(links$target, sort(unique(links$target)))-1
links
source target value source_ target_
1 1 0 1 0 0
2 2 0 8 1 0
3 3 0 10 2 0
4 3 2 6 2 1
5 4 0 1 3 0
6 5 0 1 4 0
7 6 0 1 5 0
8 7 0 1 6 0
9 8 0 2 7 0
10 9 0 1 8 0