Show only specific labels on network graph using igraph in R

风格不统一 提交于 2019-12-23 09:57:59

问题


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)

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)

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.


Examples of data

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

回答1:


Try

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))

to display only the labels for vertices with a degree greater than 2:



来源:https://stackoverflow.com/questions/32262906/show-only-specific-labels-on-network-graph-using-igraph-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!