Tal Barami
Tal Barami

Reputation: 11

R igraph: label vertex by condition

I have a graph g with a set of vertices and a list with the names of some "special" vertices. I want the graph to display a label with the vertex name ONLY for these special vertices.

I tried something like this:

plot(g, vertex.size = 4, vertex.label = ifelse(V(g) %in% usernames, V(g)$label, ""), asp = F)

But apparently I'm missing something, because the predicate never enter TRUE. Also, it seems like V(g)$label results in the following error:

Error in ans[test & ok] <- rep(yes, length.out = length(ans))[test & ok] : replacement has length zero

Your help will be appreciated! :)

Upvotes: 1

Views: 1658

Answers (1)

lukeA
lukeA

Reputation: 54247

Turn V(g) into V(g)$label?

set.seed(1)
library(igraph)
g <- ba.game(26)
V(g)$label <- letters[1:26]
usernames <- sample(letters, 5)
plot(g, vertex.label = ifelse(V(g)$label %in% usernames, V(g)$label, NA))

or

set.seed(1)
library(igraph)
g <- ba.game(26)
usernames <- sample(vcount(g), 5)
plot(g, vertex.label = ifelse(V(g) %in% usernames, V(g), NA))

?

Upvotes: 1

Related Questions