Graphing a network in igraph or other packages in R that has each node dependent on covariates?
Graphing a network in igraph or other packages in R that has each node dependent on covariates?
I currently have a network in R, available in an adjacency matrix, edgelist, and network format, that I would like to plot based on the covariates of each of the nodes. I have three covariates, age
, class
, and score
, with age
being 0 or 1, class
ranging from 1 to 50, and score
from 0 to 100. I am wondering which packages in R might facilitate plotting nodes and their network edges with node colors depending on class
, the symbol of a node depending on age
, and the size of a node dependent on the score
? Do there exists such methods in igraph
? Thanks!
age
class
score
age
class
score
class
age
score
igraph
1 Answer
1
Here is an example using igraph
. Since you do not provide any data, I illustrate with some randomly generated data.
igraph
library(igraph)
set.seed(12)
g = erdos.renyi.game(10,0.33)
V(g)$age = sample(0:1, 10, replace=TRUE)
V(g)$class = sample(50, 10, replace=TRUE)
V(g)$score = sample(0:100, 10, replace=TRUE)
plot(g, vertex.shape = c("circle", "square")[V(g)$age + 1],
vertex.color = rainbow(50)[V(g)$class],
vertex.size = round(sqrt(V(g)$score+25)),
vertex.label.dist = 1.5)
Comments
I question that one can distinguish 50 colors to read 50 values of class from the graph.
You want the node size to depend on score which goes from 0 to 100. You surely don't want any node of size zero and you surely don't want any node to be 100 times as big as another node. So I used size = round(sqrt(V(g)$score+25))
to avoid these issues.
size = round(sqrt(V(g)$score+25))
Because it was hard to read the labels when the nodes were very small, I moved the labels a bit to the side with vertex.label.dist = 1.5
vertex.label.dist = 1.5
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
wow this is great! Did not realize igraph could do such things. thanks!
– user321627
Jun 30 at 14:09