Mark
Mark

Reputation: 1769

Plot of igraph: how to highlight a set of vertices

I am working with the igraph package in R to highlight a set of vertices. E.g. in

library(igraph)
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
                        "Esmeralda"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                           "David", "Esmeralda"),
                    to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"))
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)
plot(g)

how can I highlight vertices Bob and David in the plot? In general, how can I highlight vertices that are enclosed in a vector?

Upvotes: 1

Views: 381

Answers (1)

Santanu
Santanu

Reputation: 382

You can highlight by changing the color of the vertices.

library(igraph)
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
                            "Esmeralda"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                               "David", "Esmeralda"),
                        to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"))
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)
plot(g)

############
highlight.these=c("Bob","David")
vertex.attributes(g)$color=ifelse(vertex.attributes(g)$name%in%highlight.these,"yellow","white")
plot(g)

Upvotes: 4

Related Questions