7

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
tchaymore
  • 3,728
  • 13
  • 55
  • 86
  • 3
    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 Aug 28 '15 at 04:04
  • @jlhoward Great point, thanks! Added. – tchaymore Aug 29 '15 at 14:36

1 Answers1

12

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

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • a (rather late) follow-up question on your answer and my question is closely related to this one (why I don't ask a 'new' question): If you don't use degree or betweenness-centrality, but eigenvector or hub centrality, how would your suggesstion work then (since you have to specifiy the vector to work with the values properly)? Do you (or anybody else) have an idea? – Stefan_W Oct 20 '17 at 09:25
  • @Stefan_W It is best to open a new question that showcases a small example, what you tried and why it did not work. – lukeA Oct 20 '17 at 09:40