How to scale red colors of igraph?

I draw a graph with a graph, and I would like the edges to have different colors depending on the strength of the joints that they represent. I could set the colors, but I cannot associate them with the strength values ​​of the compounds. My current code is as follows:

library(igraph)
library(raster)
library(ggplot2)
library(statnet)
library(qgraph)
connectivityMatrix <- as.matrix(read.table(file=myFile,sep='')))
coordinates <- as.matrix(read.table(file=coordinatesFile))
connectivityMatrix<-connectivityMatrix[1:833,1:833]
CM<-connectivityMatrix[subsetX,subsetY]
COORD<-coordinates[subset,]

net <- as.network(CM, matrix.type = "adjacency", directed = TRUE)

minX<-min(coordinates[,1])
maxX<-max(coordinates[,1])
minY<-min(coordinates[,2])
maxY<-max(coordinates[,2])


p<-plot(net, coord=COORD,xlim=c(minX,maxX),ylim=c(minY,maxY),edge.col=c('red','yellow','cyan','blue'),object.scale=0.005, vertex.col='dimgrey',edge.lwd=1) 

Is there a way in the above code to associate the colors specified using edge.col with the range of values ​​that they represent in CM? Thus, the edges corresponding to the value 0-x1 in the connectivity matrix will be displayed in red, x1-x2 in yellow, .... and x3-x4 in blue. x1, x2, x3 are the limits of the range, and x4 is the maximum of CM. Does anyone have an idea on how to do this? Can I add a legend, including the color of the edges and the ranges of values ​​that they represent?

Thank you in advance

+4
1

colorRamp . ., , .

library(igraph)
#Create a random weighted graph
g = erdos.renyi.game(10,0.5)
E(g)$weight = runif(ecount(g))

#Color scaling function
c_scale <- colorRamp(c('red','yellow','cyan','blue'))

#Applying the color scale to edge weights.
#rgb method is to convert colors to a character vector.
E(g)$color = apply(c_scale(E(g)$weight), 1, function(x) rgb(x[1]/255,x[2]/255,x[3]/255) )

#plot using igraph
plot.igraph(g)
+5

All Articles