-1

V(s1)["158"]$color <- "gold"

The above code changes the colour of just one node. I would like to add multiple nodes of my choice say 158,43, 87 and apply the same colour..

How do I add nodes

vidhya9
  • 27
  • 1
  • 6

1 Answers1

2

This should work, assuming 158,43,87 are the also the corresponding indices

V(s1)$color[c(43,87,158)] <- "gold"

If however, "158", "43", "87" are the vertex labels and do not correspond to indices then you can do this instead

V(s1)$color[V(s1)$label %in% c("43", "87", "158")] <- "gold"

In general you can change node colors by the following:

library(igraph)
n <-sample(5:10,1)
g <- graph.ring(n)
plot(g, vertex.label=V(g)$number)

# change all node colors
V(g)$color <- "red"

# change select node colors by indices
V(g)$color[c(1,3,5)] <- "green"
plot(g, vertex.label=V(g)$number)

# change select node colors by matching node labels
V(g)$label <- paste0("v", 1:n)
V(g)$color[V(g)$label %in% c("v1", "v5")] <- "blue"
plot(g)
Djork
  • 3,319
  • 1
  • 16
  • 27