如何减少 ggraph 弧图中的节点?
How can I reduce the nodes in a ggraph arc graph?
我正在尝试创建一个弧形图,显示非营利组织之间的关系,重点关注以其中一个非营利组织为中心的子图。这个子图中有很多非营利组织,我需要减少弧形图中的节点数量,只关注最强的连接。
我已经成功过滤掉权重低于 50 的边。但是当我创建图形时,即使边已经消失,节点仍然存在。如何从弧形图中过滤不需要的节点?[=12=]
这是我的代码,从创建 igraph 对象开始。
# Create an igraph object
NGO_igraph <- graph_from_data_frame(d = edges, vertices = nodes, directed = TRUE)
# Create a subgraph centered on a node
# Start by entering the node ID
nodes_of_interest <- c(48)
# Build the graph
selegoV <- ego(NGO_igraph, order=1, nodes = nodes_of_interest, mode = "all", mindist = 0)
selegoG <- induced_subgraph(NGO_igraph,unlist(selegoV))
# Reducing the graph based on edge weight
smaller <- delete.edges(selegoG, which(E(selegoG)$weight < 50))
# Plotting an arc graph
ggraph(smaller, layout = "linear") +
geom_edge_arc(aes(width = weight), alpha = 0.8) +
scale_edge_width(range = c(0.2, 2)) +
geom_node_text(aes(label = label)) +
labs(edge_width = "Interactions") +
theme_graph()
这是我得到的结果:
在没有任何示例数据的情况下,我创建了这个基本示例:
#define graph
g <- make_ring(10) %>%
set_vertex_attr("name", value = LETTERS[1:10])
g
V(g)
#delete edges going to and from vertice C
g<-delete.edges(g, E(g)[2:3])
#find the head and tails of each edge in graph
heads<-head_of(g, E(g))
tails<-tail_of(g, E(g))
#list of all used vetrices
combine<-unique(c(heads, tails))
#collect an vertices
v<-V(g)
#find vertices not in found set
toremove<-setdiff(v, combine)
#remove unwanted vertices
delete_vertices(g, toremove)
基本过程是识别所有感兴趣边的起点和终点,然后将此唯一列表与所有边进行比较,并删除不在唯一列表中的边。
从您上面的代码中,图表 "smaller" 将用于查找顶点。
希望对您有所帮助。
如果您只对省略零度顶点或孤立点(即没有传入或传出边的顶点)感兴趣,您可以简单地使用以下行:
g <- induced.subgraph(g, degree(g) > 0)
但是,这将删除 所有 株。因此,如果您出于某种原因设置专门删除由小于 50 的边连接的 那些 顶点(并免除其他 'special' 隔离),那么您将需要清楚地识别哪些是:
special_vertex <- 1
v <- ends(g, which(E(g) < 50))
g <- delete.vertices(g, v[v != special_vertex])
您也可以通过考虑顶点的 strength
来跳过 delete.edges
部分:
g <- induced.subgraph(g, strength(g) > 50)
我正在尝试创建一个弧形图,显示非营利组织之间的关系,重点关注以其中一个非营利组织为中心的子图。这个子图中有很多非营利组织,我需要减少弧形图中的节点数量,只关注最强的连接。
我已经成功过滤掉权重低于 50 的边。但是当我创建图形时,即使边已经消失,节点仍然存在。如何从弧形图中过滤不需要的节点?[=12=]
这是我的代码,从创建 igraph 对象开始。
# Create an igraph object
NGO_igraph <- graph_from_data_frame(d = edges, vertices = nodes, directed = TRUE)
# Create a subgraph centered on a node
# Start by entering the node ID
nodes_of_interest <- c(48)
# Build the graph
selegoV <- ego(NGO_igraph, order=1, nodes = nodes_of_interest, mode = "all", mindist = 0)
selegoG <- induced_subgraph(NGO_igraph,unlist(selegoV))
# Reducing the graph based on edge weight
smaller <- delete.edges(selegoG, which(E(selegoG)$weight < 50))
# Plotting an arc graph
ggraph(smaller, layout = "linear") +
geom_edge_arc(aes(width = weight), alpha = 0.8) +
scale_edge_width(range = c(0.2, 2)) +
geom_node_text(aes(label = label)) +
labs(edge_width = "Interactions") +
theme_graph()
这是我得到的结果:
在没有任何示例数据的情况下,我创建了这个基本示例:
#define graph
g <- make_ring(10) %>%
set_vertex_attr("name", value = LETTERS[1:10])
g
V(g)
#delete edges going to and from vertice C
g<-delete.edges(g, E(g)[2:3])
#find the head and tails of each edge in graph
heads<-head_of(g, E(g))
tails<-tail_of(g, E(g))
#list of all used vetrices
combine<-unique(c(heads, tails))
#collect an vertices
v<-V(g)
#find vertices not in found set
toremove<-setdiff(v, combine)
#remove unwanted vertices
delete_vertices(g, toremove)
基本过程是识别所有感兴趣边的起点和终点,然后将此唯一列表与所有边进行比较,并删除不在唯一列表中的边。
从您上面的代码中,图表 "smaller" 将用于查找顶点。
希望对您有所帮助。
如果您只对省略零度顶点或孤立点(即没有传入或传出边的顶点)感兴趣,您可以简单地使用以下行:
g <- induced.subgraph(g, degree(g) > 0)
但是,这将删除 所有 株。因此,如果您出于某种原因设置专门删除由小于 50 的边连接的 那些 顶点(并免除其他 'special' 隔离),那么您将需要清楚地识别哪些是:
special_vertex <- 1
v <- ends(g, which(E(g) < 50))
g <- delete.vertices(g, v[v != special_vertex])
您也可以通过考虑顶点的 strength
来跳过 delete.edges
部分:
g <- induced.subgraph(g, strength(g) > 50)