user10293275
user10293275

Reputation: 47

How can I change the vertex color when plotting a network using igraph depending on specific actor attribute?

I am estimating networks with ERGMs using the statnet suite in R. I would like to visualize the network using the igraph package with size by degree centrality and node color by leadership position. For explanation: it's a shared leadership network and I want to visualize whether an individual has a formal leadership position (LSPosition = 1), visualized in black, or not (LSPosition = 0), visualized in white.

This is my code up to now (graphExample is my network, Data_Axample is an actor attribute data set):

library(igraph)
degreeExample <- centralization.degree(graphExample)$res
    V(graphExample)$size <- degreeExample
    V(graphExample)$LSPosition <- Data_Example$LSPosition
    colrs <- colors(c("black","white"))
    V(graphExample)$color <- colrs[V(graphExample)$LSPosition]
    E(graphExample)$arrow.size <- 0.5
    plot(graphExample, vertex.label = NA, vertex.label.family = "Arial")
    legend("bottomleft", c("Employee in no leading position",
                           "Employee in a leading position"), 
           pch = 21, col = "#777777", pt.bg = colrs, pt.cex = 2, cex = 0.8, bty = "n", ncol = 1)

The problem is, that for colrs <- colors(c("black","white")) I get the following error:

Error in if (distinct) c[!duplicated(t(col2rgb(c)))] else c : 
  argument is not interpretable as logical
In addition: Warning message: In if (distinct) c[!duplicated(t(col2rgb(c)))] else c : 
the condition has length > 1 and only the first element will be used

I also tried the following:

V(graphExample)$color <- ifelse(V(graphExample)$LSPosition==1, "black", ifelse(V(graphExample)$LSPosition==0, "white"))

But I get the error:

Error in ifelse(V(graphSLO_V1)$PositionO == 0, "white") : argument "no" is missing, with no default.

How can I set the colors?

Upvotes: 0

Views: 633

Answers (1)

G5W
G5W

Reputation: 37621

There are two problems here. First, the colors function lists all of the available colors by name. I think that you are just trying to get a list with the colors "black" and "white. That is simpler than what you tried. All you need is colrs <- c("black","white") . But according to your description, V(graphExample)$LSPosition will have values of either 0 or 1. The list colrs should be indexed by 1 or 2. The simple thing to do would be to simply shift the indices by using

V(graphExample)$color <- colrs[V(graphExample)$LSPosition + 1]

But that will make (LSPosition = 0) be black (you wanted white) and (LSPosition = 1) be white (you wanted black). So I would use the line with the +1 as above, but change the definition of colrs to colrs <- c("white", "black")

Upvotes: 1

Related Questions