igraph边缘宽度和颜色正负相关

时间:2018-03-08 11:25:40

标签: r igraph

我正在尝试使用分类信息绘制i图。原始相关矩阵作为输入输出附加。

The original dput file correlation matrixe is found here

我的代码看起来像这样。假设cor.matrix是相关矩阵(参见获取它的链接)。

set.seed(123)

  t = which(abs(cor.matrix) > 0.6 & lower.tri(cor.matrix),arr.ind=TRUE)
  t.graph=graph.data.frame(t,directed=F)
  E(t.graph)$color =ifelse(cor.matrix[t] > 0.6,'magenta','green')

  t.names <- colnames(cor.matrix)[as.numeric(V(t.graph)$name)]
  minC <- rep(-Inf, vcount(t.graph))
  maxC <- rep(Inf, vcount(t.graph))
  minC[1] <- maxC[1] <- 0
  l <- layout_with_fr(t.graph, minx=minC, maxx=maxC,
                       miny=minC, maxy=maxC)      
  plot(t.graph, layout=l, 
       rescale=T,
       asp=0,
       edge.arrow.size=0.5, 
       vertex.label.cex=0.8, 
       vertex.label.family="Helvetica",
       vertex.label.font=2,
       vertex.label=t.names,
       vertex.shape="circle", 
       vertex.size=3, 
       vertex.color="deepskyblue2",
       vertex.label.color="black", 
       edge.width=0.5)

enter image description here

我想完成三件事:

1 - 有一种颜色用于正相关和负相关。

2 - 改变边缘宽度,边缘越厚,相关性越高(正或负)。

3 - 让图表更易于查看,难以查看和识别节点

非常感谢。

1 个答案:

答案 0 :(得分:0)

对于第1点和第2点,您只需将向量传递给相应的绘图参数即可。对于边缘颜色使用edge.color和边缘宽度使用edge.width,您还可以在图形对象中设置边缘属性,igraph将自动将其用于绘图。 igraph手册详细说明了这一点:http://igraph.org/r/doc/plot.common.html

至于第3点。使图形更具可读性的唯一方法是将其绘制到更大的画布上(即增加分辨率)或删除一些节点。随着节点数量的增加,图形变得非常难以阅读,并且无关紧要。

library(igraph)
set.seed(123)

cor.matrix <- matrix(runif(100, -1, 1), nrow=10)

t = which(abs(cor.matrix) > 0.6 & lower.tri(cor.matrix),arr.ind=TRUE)
t <- cbind(t, cor.matrix[which(abs(cor.matrix) > 0.6 & lower.tri(cor.matrix),arr.ind=TRUE)]) ##this adds the correlation to the graph as an edge attribute "V3"
t.graph=graph.data.frame(t,directed=F)
E(t.graph)$color <- ifelse(E(t.graph)$V3 > 0,'magenta','green') #You had this as "V3 > 0.6" which I guess works but it is more readable as 0. that way if you decide to lower the correlation threshold you do not have to change this line too.

#t.names <- colnames(cor.matrix)[as.numeric(V(t.graph)$name)]
minC <- rep(-Inf, vcount(t.graph))
maxC <- rep(Inf, vcount(t.graph))
minC[1] <- maxC[1] <- 0
l <- layout_with_fr(t.graph, minx=minC, maxx=maxC,
                    miny=minC, maxy=maxC)      
plot(t.graph, layout=l, 
     rescale=T,
     asp=0,
     edge.arrow.size=0.5, 
     vertex.label.cex=0.8, 
     vertex.label.family="Helvetica",
     vertex.label.font=2,
     #vertex.label=t.names,
     vertex.shape="circle", 
     vertex.size=3, 
     vertex.color="deepskyblue2",
     vertex.label.color="black", 
     #edge.color=E(t.graph)$color, ##do not need this since E(t.graph)$color is already defined.
     edge.width=as.integer(cut(abs(E(t.graph)$V3), breaks = 5)))

enter image description here

相关问题