基于边权重的子图
Subset graph based on edges weight
我有一个图 G=(V,E),它有几个属性,包括边权重属性。我正在尝试根据权重高于 x 的条件创建子图。
我已经尝试使用 g <- E(g)[weight > max(weight)*.10]
的标准 R 子集选项,但我总是得到一个矢量。
我不确定我在这里做错了什么。
也许你想要这样的东西
library(igraph)
set.seed(1)
m <- matrix(sample(c(.5, 2, 5), 100, replace=T, prob = c(.6,.3,.1)), nc=10, dimnames = rep(list(letters[1:10]), 2))
g <- graph_from_adjacency_matrix(m, weighted=T, diag=F, mode="undirected")
coords <- layout.auto(g)
par(mfrow = c(1,3))
plot(g, layout=coords, edge.width = E(g)$weight)
s1 <- subgraph.edges(g, E(g)[E(g)$weight>2], del=F)
plot(s1, layout=coords, edge.width = E(s1)$weight)
s2 <- delete_vertices(s1, degree(s1, mode = "in")==0)
plot(s2, layout=coords[V(g)$name%in%V(s2)$name,], edge.width = E(s2)$weight)
那是因为您仅用子集边替换了图 g。如果要去除低于阈值权重的边,可以使用:
g_sub <- delete.edges(g, E(g)[weight <= max(weight)*.10])
我有一个图 G=(V,E),它有几个属性,包括边权重属性。我正在尝试根据权重高于 x 的条件创建子图。
我已经尝试使用 g <- E(g)[weight > max(weight)*.10]
的标准 R 子集选项,但我总是得到一个矢量。
我不确定我在这里做错了什么。
也许你想要这样的东西
library(igraph)
set.seed(1)
m <- matrix(sample(c(.5, 2, 5), 100, replace=T, prob = c(.6,.3,.1)), nc=10, dimnames = rep(list(letters[1:10]), 2))
g <- graph_from_adjacency_matrix(m, weighted=T, diag=F, mode="undirected")
coords <- layout.auto(g)
par(mfrow = c(1,3))
plot(g, layout=coords, edge.width = E(g)$weight)
s1 <- subgraph.edges(g, E(g)[E(g)$weight>2], del=F)
plot(s1, layout=coords, edge.width = E(s1)$weight)
s2 <- delete_vertices(s1, degree(s1, mode = "in")==0)
plot(s2, layout=coords[V(g)$name%in%V(s2)$name,], edge.width = E(s2)$weight)
那是因为您仅用子集边替换了图 g。如果要去除低于阈值权重的边,可以使用:
g_sub <- delete.edges(g, E(g)[weight <= max(weight)*.10])