Hi everyone,
Thanks @tamas for your reply. As you state, the ‘Iayout=’ argument accepts matrices with the coordinates. I am collaborating with @umerlone in this work and I wanted to share with the community how we replicated the output in ‘igraph’ for R. I’d really appreciate suggestions!
library(igraph)
set.seed(77) #for reproducible example
g <- barabasi.game(30, 1.3, directed = FALSE) #generate a scale-free graph
#generate some node attributes
V(g)$attr1 <- sample(letters[1:3], 30, replace = TRUE)
V(g)$attr2 <- sample(c('high','medium','low'), 30, replace = TRUE)
We now have a graph with 50 nodes with two different categorical attributes having three levels each. We want to create a plot where the nodes are clustered depending on their attributes. In particular, attr1 will define the longitude coordinates of the nodes, and attr2 will define their latitude. Let’s create the coordinate.
#generate coordinates for the x-axis
coordx = ifelse(V(g)$attr1 == 'a', runif(length(V(g)$attr1), min= -2, max = -1),
ifelse(V(g)$attr1 =='b', runif(length(V(g)$attr1), min= 0, max = 1),
runif(length(V(g)$attr1), min= 2, max = 3)))
#generate coordinates for the y-axis
coordy = ifelse(V(g)$attr2 == 'low', runif(length(V(g)$attr2), min= -1, max = 0),
ifelse(V(g)$attr2 =='medium', runif(length(V(g)$attr2), min= 1, max = 2),
runif(length(V(g)$attr2), min= 3, max = 4)))
As you can see, we let the system generate some random coordinates from a given interval both for the x and y-axis. Each level of the attributes has its own interval. I know a little about coordinates for graphical outputs in R, but this solution worked pretty well for us.
#some color for the plot
V(g)[V(g)$attr1 == 'a']$color <- "#008080"
V(g)[V(g)$attr1 == 'b']$color <- "#808080"
V(g)[V(g)$attr1 == 'c']$color <- "#DAA520"
# create the 2 x 2 matrix with N= vcount(g)
m <- matrix(0, vcount(g), 2)
m[,1] = coordx
m[,2] = coordy
#and plot the graph using the matrix m as the layout argument
plot(g, layout = m, vertex.size = 5, vertex.label = NA)
# simple axis
axis(1, , at = c(-1, 0, 1), labels = c('a','b','c'))
axis(2, at = c(-0.75, 0, 0.75), labels = c('low', 'medium','high'))
Actually, for a better graphical representation, we used the ‘popgraph’ library with ‘ggplot2’, but this example is a good start. Ideally, it’s possible to add attributes.
This is my first post, I hope to have followed the guidelines.
Thanks
Matteo