【问题标题】:How to scale edge colors in igraph?如何在 igraph 中缩放边缘颜色?
【发布时间】:2015-04-06 15:04:46
【问题描述】:

我正在用 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 的最大值。

有人知道怎么做吗?是否可以添加一个图例,包括边缘的颜色和它们所代表的值范围?

【问题讨论】:

    标签: r plot colors igraph


    【解决方案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)
    

    【讨论】:

    • 谢谢,这是我一直在寻找的那种功能。同时,我根据 CM 的值创建了一个具有指定颜色的矩阵,并将其作为 edge.col 参数的参数传递。这使我能够创建一个漂亮的图例并将 CM 中的值范围与颜色相关联。还是谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多