5
votes

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
You've been on SO for awhile, so you should know by now that providing your data, or at least a representative sample, is expected. Otherwise we have to make up data for you, to demonstrate a solution.jlhoward
@jlhoward Great point, thanks! Added.tchaymore

1 Answers

9
votes

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:

enter image description here