如何缩放 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 以 'yellow',.... 和 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)
我正在用 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 以 'yellow',.... 和 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)