Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make edge weight, colour and line-type dependent on edge-value using igraph?

Tags:

r

igraph

I am trying to plot a network, showing the dependency between 17 plant traits. The network will be used to help interpret result form some other analyses, not giving direction to the dependencies themselves.

I am using igraph in R, with a 17x17 correlation matrix as input. The correlations in the matrix were altered so that the first digit represent a class. Although I am fairly new to R, it has been going well in the sense that I can almost produce the graph I envisioned.

However, I can't find a way to make the edge style dependent on the value of the correlation. I can do so for colour and weight, but adding lty or edge.lty in a weight dependent manner doesn't result in the desired graph, although the code runs without error. The idea is to use 3 line types, so that values between -4 and -3 have a different style from those with values between -3 and -2 etc... The 'groups' represent correlations present only in treatment x, or only in y etc.

Looking around here for help many times, I haven't encountered this problem, so I thought I'd ask.

The code I have been using (below) is perhaps not pretty, but for an inexperienced user like myself it least works :)

I tried (among others) adding E(graphY1W)[weight <= -4.0 & weight < -3.75]$lty <- 3 #dotted. However, as said, the code runs without error, but the line is still solid. Can it be done?

graphY1W<-graph.adjacency(Y1W,weighted=TRUE,mode="undirected",diag=FALSE)
E(graphY1W)[weight <= -4.0 & weight < -3.75]$color <- "black"      #strong - relationship group1
E(graphY1W)[weight <= -4.0 & weight < -3.75]$width <- 7
E(graphY1W)[weight >= -3.75 & weight <= -3.5 ]$color <- "black"    #weak - relationship group1
E(graphY1W)[weight >= -3.75 & weight <= -3.5 ]$width <- 3
E(graphY1W)[weight <=  4.0 & weight > 3.75 ]$color <- "grey"       #strong + relationship group1
E(graphY1W)[weight <=  4.0 & weight > 3.75 ]$width <- 7
E(graphY1W)[weight <= 3.75 & weight >= 3.5 ]$color <- "grey"      #weak + relationship group1
E(graphY1W)[weight <= 3.75 & weight >= 3.5 ]$width <- 3
E(graphY1W)[weight >= -3.0 & weight < -2.75]$color <- "black"     #strong - relationship group2
E(graphY1W)[weight >= -3.0 & weight < -2.75]$width <- 7
E(graphY1W)[weight >= -2.75 & weight <= -2.5 ]$color <- "black"   #etc
E(graphY1W)[weight >= -2.75 & weight <= -2.5 ]$width <- 3
E(graphY1W)[weight <=  3.0 & weight > 2.75 ]$color <- "grey"
E(graphY1W)[weight <=  3.0 & weight > 2.75 ]$width <- 7
E(graphY1W)[weight <= 2.75 & weight >= 2.5 ]$color <- "grey"
E(graphY1W)[weight <= 2.75 & weight >= 2.5 ]$width <- 3
E(graphY1W)[weight >= -2.0 & weight < -1.75 ]$color <- "black" 
E(graphY1W)[weight >= -2.0 & weight < -1.75 ]$width <- 7
E(graphY1W)[weight >= -1.75 & weight <= -1.5 ]$color <- "black"
E(graphY1W)[weight >= -1.75 & weight <= -1.5 ]$width <- 3
E(graphY1W)[weight <= 2.0 & weight >  1.75 ]$color <- "grey"
E(graphY1W)[weight <= 2.0 & weight >  1.75 ]$width <- 7
E(graphY1W)[weight <= 1.75 & weight >= 1.5 ]$color <- "grey"
E(graphY1W)[weight <= 1.75 & weight >= 1.5 ]$width <- 3
V(graphY1W)$color <-  ifelse(V(graphY1W)$name=="Pn", "grey",
                      ifelse(V(graphY1W)$name=="gs", "grey",
                      ifelse(V(graphY1W)$name=="Pn_amb.Pn_sat", "grey",
                      ifelse(V(graphY1W)$name=="WUE", "grey",
                      ifelse(V(graphY1W)$name=="WP", "grey",
                      ifelse(V(graphY1W)$name=="TL", "grey95",
                      ifelse(V(graphY1W)$name=="FMDM", "grey95",
                      ifelse(V(graphY1W)$name=="Chl", "grey45",
                      ifelse(V(graphY1W)$name=="Chlab", "grey45",
                      ifelse(V(graphY1W)$name=="Car", "grey45",
                      ifelse(V(graphY1W)$name=="MP", "white",
                      ifelse(V(graphY1W)$name=="LMF", "white",
                      ifelse(V(graphY1W)$name=="SRR", "white",
                      ifelse(V(graphY1W)$name=="SLAP", "grey95",
                      ifelse(V(graphY1W)$name=="AP", "grey95",
                      ifelse(V(graphY1W)$name=="NAR", "white",
                      ifelse(V(graphY1W)$name=="RGR", "white","white")))))))))))))))))
plot(graphY1W,layout=layout.fruchterman.reingold, vertex.color=V(graphY1W)$color,
     vertex.label.color="black", vertex.shape="circle",vertex.size=10, vertex.label.cex=0.9,
     asp=0.5, frame=FALSE)
like image 949
The Riddler Avatar asked Oct 01 '22 03:10

The Riddler


1 Answers

I am not sure what you are doing wrong, but here is how to do it.

library(igraph)
g <- graph.ring(10)
E(g)$lty <- 1:5
plot(g)

graph plot

You probably don't have any dotted edges, that's why they don't show up. Btw. weight <= -4.0 & weight < -3.75 is redundant, maybe you want weight > -3.75.

like image 176
Gabor Csardi Avatar answered Oct 11 '22 06:10

Gabor Csardi