繁体   English   中英

如何在igraph中缩放边缘颜色?

[英]How to scale edge colors in igraph?

我正在用 igraph 绘制一个图形,我希望边缘具有不同的颜色,具体取决于它们所代表的连接强度。 我可以设置颜色,但我无法将它们与连接强度的值联系起来。

我目前的代码如下:

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) 

在上面的代码中,有没有办法将使用 edge.col 指定的颜色与它们在 CM 中表示的值范围相关联? 这样,与连接矩阵中值 0-x1 对应的边将绘制为红色,x1-x2 绘制为“黄色”,.... x3-x4 绘制为蓝色。 x1、x2、x3 是范围限制,x4 是 CM 的最大值。

有没有人知道如何做到这一点? 是否可以添加一个图例,包括边缘的颜色和它们代表的值的范围?

您可以使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM