adR
adR

Reputation: 325

how to plot network graph by coloring the nodes

I have a data frame with two columns(edgelist)and I want to color the two columns in two different colors in the igraph plots. Like the capital letter node in blue and the small letter in dark read. My code is below.

dat <- data.frame(col1 = letters[1:20], col2 = LETTERS[1:20])
library(igraph)
g <- graph.edgelist(as.matrix(dat ),directed = T)
set.seed(2021)
plot(g, layout=  layout_nicely(g))

Thanks!

Upvotes: 1

Views: 545

Answers (1)

Ashish
Ashish

Reputation: 367

You can call out the graph element and assign the color based on the condition of whether the value is upper/lower case.

V(g)$color <- ifelse(V(g)$name==toupper(V(g)$name),"blue","red")

Try plotting the graph after this line

Upvotes: 1

Related Questions