2015-02-06 98 views
4

我正在用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的最大值。 有没有人有关于如何做到这一点的想法?是否可以添加一个图例,包括边缘的颜色和它们代表的值的范围?

预先感谢您,

回答

5

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

感谢这是我一直在寻找的功能。同时,我使用根据CM的值指定的颜色创建了一个矩阵,并将其作为edge.col参数的参数传递。这使我可以创建一个不错的图例,并将CM中的值范围与颜色关联起来。不管怎样,谢谢你。 – user3584444 2015-02-16 13:07:46

+0

不客气。不要忘记接受答案。 – ahmohamed 2015-02-16 13:20:47