[英]igraph edge width and color positive and negative correlation
我正在嘗試使用分類信息繪制i圖。 原始相關矩陣作為Dput輸出附加。
我的代碼如下所示。 假設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)
我想完成三件事:
1-具有正負相關的一種顏色。
2-更改邊緣寬度,相關性(正或負)越高,邊緣越厚。
3-具有易於查看的圖形,難以查看和識別節點
非常感謝。
對於點1和2,您只需要將向量傳遞給適當的繪圖參數即可。 對於邊緣顏色,請使用edge.color
;對於邊緣寬度,請使用edge.width
您還可以在圖形對象中設置edge屬性,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)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.