鉴于数据框:
> dput(rel.matrix)
structure(list(from = c("A", "A", "A", "A", "B", "B", "B", "B",
"C", "C", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E",
"E", "F", "F", "F", "F", "F", "F", "G", "G", "G", "G", "H", "H",
"H", "H", "I", "I", "I", "I", "J", "J", "J", "J", "K", "K"),
to = c("B", "C", "D", "E", "A", "C", "D", "E", "A", "B",
"D", "E", "F", "K", "A", "B", "C", "E", "A", "B", "C", "D",
"C", "G", "H", "I", "J", "K", "F", "H", "I", "J", "F", "G",
"I", "J", "F", "G", "H", "J", "F", "G", "H", "I", "C", "F"
), weight = c(0.09137056, 0.2677665, 0.09137056, 0.09137056,
0.09137056, 0.09517766, 0.02284264, 0.02284264, 0.2677665,
0.09517766, 0.09517766, 0.09517766, 0.3730964, 0.1675127,
0.09137056, 0.02284264, 0.09517766, 0.02284264, 0.09137056,
0.02284264, 0.09517766, 0.02284264, 0.3730964, 0.09517766,
0.09517766, 0.2677665, 0.09517766, 0.1675127, 0.09517766,
0.02284264, 0.09137056, 0.02284264, 0.09517766, 0.02284264,
0.09137056, 0.02284264, 0.2677665, 0.09137056, 0.09137056,
0.09137056, 0.09517766, 0.02284264, 0.02284264, 0.09137056,
0.1675127, 0.1675127)), row.names = c(NA, -46L), class = "data.frame")
和两组节点
> dput(soi)
c("A", "I", "G")
> dput(all_nodes)
c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K")
我具有以下功能:对权重应用阈值'节点,在soi
和all_nodes
变量的节点之间给出不同的节点颜色,最后绘制图形。
现在的情况是,正如您所看到的,阈值应用于所有节点'权重,无论它们属于哪个集合(soi
OR all_nodes
)。结果是从图中删除属于soi
集的顶点,这是我不想要的。
实际上,我想绘制所有属于soi
集的顶点并使用threshold
过滤掉那些不属于soi
的{{1}}的顶点setdiff(all_nodes,soi)
。
这是功能。
graph_rel_network <- function ( rel.matrix, nodes.names, soi.names, threshold )
{
# setup vertices meta data which contain soi and not soi
meta <- cbind( nodes.names, issoi = nodes.names %in% soi.names )
# read in a graph: the weights are in the edge attributes
g <- igraph::graph_from_data_frame(rel.matrix, directed = TRUE, vertices = meta)
g <- as.undirected(g, mode = "collapse")
# rescale weights
E(g)$weight2 <- 9 * E(g)$weight / max(E(g)$weight)
# remove edges acoarding to the threshold
g_sub <- delete.edges(g, E(g)[weight2 <= threshold ])
# remove vertices with 0 degree
g_sub <- delete.vertices(simplify(g_sub), degree(g_sub)==0)
# color vertices that belongs to soi
V(g_sub)$color <- ifelse(V(g_sub)$issoi == TRUE, "gold", "tomato")
# plot the graph
plot.igraph( g_sub,
edge.width=E(g_sub)$weight2,
vertex.label.dist=0,
vertex.frame.color="gray",
vertex.label.color="black")
legend(x=-1.5, y=-1.1,
c("Nodes of interest","Most Relevant nodes"),
pch=21,
pt.bg= c('gold','tomato'),
pt.cex=2,
cex=1,
bty="n",
ncol=1)
}
例如,您可以执行graph_rel_network( rel.matrix, all_nodes, soi, 3)
并且在最终的绘图节点中,G
属于soi
时不会出现。{/ p>
有没有办法不过滤soi
个节点?
答案 0 :(得分:1)
要保留所有soi
个节点,您只需要为delete.vertices
调用添加一些逻辑:
graph_rel_network <- function (rel.matrix, nodes.names, soi.names, threshold )
{
# setup vertices meta data which contain soi and not soi
###changed meta to a data.frame so that issoi is stored as logical instead of character
meta <- data.frame(nodes.names, issoi = nodes.names %in% soi.names )
# read in a graph: the weights are in the edge attributes
g <- igraph::graph_from_data_frame(rel.matrix, directed = TRUE, vertices = meta)
g <- as.undirected(g, mode = "collapse")
# rescale weights
E(g)$weight2 <- 9 * E(g)$weight / max(E(g)$weight)
###Create a subnetwork of soi nodes only
g_soi <- induced_subgraph(g, vids = V(g)[V(g)$name %in% soi.names])
# remove edges acoarding to the threshold
g_sub <- delete.edges(g, E(g)[E(g)$weight2 <= threshold])
# remove vertices with 0 degree
g_sub <- delete.vertices(g_sub, (degree(g_sub)==0 & !V(g)$issoi))
###Merge the tresholded allnode network with the soi network
g_merge <- union(g_sub, g_soi)
###Resolve some attribute naming conflicts from the merge
E(g_merge)$weight2 <- colMeans(rbind(E(g_merge)$weight2_2, E(g_merge)$weight2_1), na.rm = T)
# color vertices that belongs to soi
V(g_merge)$color <- ifelse(V(g_merge)$issoi_1, "gold", "tomato")
# plot the graph
plot.igraph( g_merge,
edge.width=E(g_merge)$weight2,
vertex.label.dist=0,
vertex.frame.color="gray",
vertex.label.color="black")
legend(x=-1.5, y=-1.1,
c("Nodes of interest","Most Relevant nodes"),
pch=21,
pt.bg= c('gold','tomato'),
pt.cex=2,
cex=1,
bty="n",
ncol=1)
}