简体   繁体   English

图形边缘宽度与颜色正负相关

[英]igraph edge width and color positive and negative correlation

I´m trying to plot an i graph using taxonomic information. 我正在尝试使用分类信息绘制i图。 The original correlation matrix is attached as a dput output. 原始相关矩阵作为Dput输出附加。

The original dput file correlation matrixe is found here 原始dput文件相关矩阵位于此处

My code looks like this. 我的代码如下所示。 Assuming cor.matrix is the correlation matrix (see link to get it). 假设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)

在此处输入图片说明

I would like to accomplish three different things: 我想完成三件事:

1 - Have one color for positive and negative correlation. 1-具有正负相关的一种颜色。

2 - Change edge width, the higher the correlation (positive or negative) the thicker the edge. 2-更改边缘宽度,相关性(正或负)越高,边缘越厚。

3 - Have a graph easier to vew, it´s hard to view and identify nodes 3-具有易于查看的图形,难以查看和识别节点

Thanks so much. 非常感谢。

For points 1 and 2 you just need to pass a vector to the appropriate plotting argument. 对于点1和2,您只需要将向量传递给适当的绘图参数即可。 For edge color use edge.color and for edge width use edge.width you can also set an edge attribute in the graph object and igraph will automatically use that for plotting. 对于边缘颜色,请使用edge.color ;对于边缘宽度,请使用edge.width您还可以在图形对象中设置edge属性,igraph会自动将其用于绘图。 The igraph manual specifies a lot of this: http://igraph.org/r/doc/plot.common.html igraph手册对此进行了详细说明: http : //igraph.org/r/doc/plot.common.html

As for point 3. The only way to make the graph more readable is to make plot it to a bigger canvas (ie increase the resolution) or remove some nodes. 至于第3点,使图形更具可读性的唯一方法是将其绘制到更大的画布上(即提高分辨率)或删除一些节点。 Graphs become notoriously hard to read as the number of nodes increases and there is nothing to do about it. 众所周知,随着节点数量的增加,图形变得难以阅读,并且与此无关。

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)))

在此处输入图片说明

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

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