在R中使用igraph仅在网络图上显示特定标签 [英] Show only specific labels on network graph using igraph in R
问题描述
我正在尝试绘制仅显示某些顶点标签的图形.在这种情况下,我只想显示具有一定数量边的顶点的标签.
I'm trying to plot a graph that only displays the labels for certain vertices. In this case, I want to only display labels for vertices with a certain number of edges.
我正在将顶点和边读入图形对象,如下所示:
I'm reading vertices and edges into the graph object like so:
nodes <- read.csv("path_to_file.csv")
edges <- read.csv("path_to_file.csv")
g <- graph_from_data_frame(edges,directed=TRUE,vertices=nodes)
我使用以下命令来绘制图形并根据连接数($ rels属性是两个顶点之间的连接数)来改变边的宽度:
I use the following command to plot the graph and vary the width of the edge based on number of connections (the $rels attribute is the number of connections between two vertices):
plot.igraph(g,vertex.size=3,vertex.label.cex=0.5,layout=layout.fruchterman.reingold(g,niter=10000),edge.arrow.size=0.15,edge.width=E(g)$rels/100)
例如,是否有一种方法可以说,只有> 100条边的顶点才应显示其标签?如果我尝试在csv文件中保留顶点标签,igraph会认为它们是重复的顶点.
Is there a way to say, for instance, that only vertices with > 100 edges should have their label displayed? If I try to leave vertex labels out in my csv files, igraph thinks they are duplicate vertices.
数据示例
nodes.csv
name | org_id
U.S. Department of Energy | 70063
Environmental Protection Agency | 100000
edges.csv
from | to | rels
U.S. Department of Energy | Hanford SSAB | 477
Natural Resources Defense Council | Environmental Protection Agency | 322
推荐答案
尝试
library(igraph)
set.seed(1)
g <- sample_pa(20)
V(g)$label <- letters[1:20]
plot(g, vertex.label = ifelse(degree(g) > 2, V(g)$label, NA))
仅显示度数大于2的顶点的标签:
to display only the labels for vertices with a degree greater than 2:
这篇关于在R中使用igraph仅在网络图上显示特定标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!