如何使用igraph制作边缘重量,颜色和线型依赖于边缘值?

时间:2014-08-05 14:53:48

标签: r igraph edge

我正在尝试绘制网络,显示17种植物性状之间的依赖关系。该网络将用于帮助解释结果形式的其他一些分析,而不是指示依赖性本身。

我在R中使用igraph,使用17x17相关矩阵作为输入。矩阵中的相关性被改变,因此第一个数字代表一个类。虽然我对R来说相当新,但从某种意义上来说它一直很好,我几乎可以生成我想象的图形。

但是,我无法找到一种方法来使边缘样式依赖于相关值。我可以这样做颜色和重量,但是以重量依赖的方式添加lty或edge.lty并不会产生所需的图形,尽管代码运行没有错误。这个想法是使用3种线型,因此-4和-3之间的值与-3和-2之间的值具有不同的样式等等。'组'表示仅在治疗x中存在或仅在y等中存在的相关性。

多次在这里寻求帮助,我还没有遇到这个问题,所以我想我会问。

我一直在使用的代码(下面)可能并不漂亮,但对于像我这样缺乏经验的用户来说,它最不起作用:)

我尝试(除此之外)添加E(graphY1W)[重量< = -4.0&重量< -3.75] $ lty< - 3 #dotted。但是,如上所述,代码运行没有错误,但该行仍然是可靠的。可以吗?

graphY1W<-graph.adjacency(Y1W,weighted=TRUE,mode="undirected",diag=FALSE)
E(graphY1W)[weight <= -4.0 & weight < -3.75]$color <- "black"      #strong - relationship group1
E(graphY1W)[weight <= -4.0 & weight < -3.75]$width <- 7
E(graphY1W)[weight >= -3.75 & weight <= -3.5 ]$color <- "black"    #weak - relationship group1
E(graphY1W)[weight >= -3.75 & weight <= -3.5 ]$width <- 3
E(graphY1W)[weight <=  4.0 & weight > 3.75 ]$color <- "grey"       #strong + relationship group1
E(graphY1W)[weight <=  4.0 & weight > 3.75 ]$width <- 7
E(graphY1W)[weight <= 3.75 & weight >= 3.5 ]$color <- "grey"      #weak + relationship group1
E(graphY1W)[weight <= 3.75 & weight >= 3.5 ]$width <- 3
E(graphY1W)[weight >= -3.0 & weight < -2.75]$color <- "black"     #strong - relationship group2
E(graphY1W)[weight >= -3.0 & weight < -2.75]$width <- 7
E(graphY1W)[weight >= -2.75 & weight <= -2.5 ]$color <- "black"   #etc
E(graphY1W)[weight >= -2.75 & weight <= -2.5 ]$width <- 3
E(graphY1W)[weight <=  3.0 & weight > 2.75 ]$color <- "grey"
E(graphY1W)[weight <=  3.0 & weight > 2.75 ]$width <- 7
E(graphY1W)[weight <= 2.75 & weight >= 2.5 ]$color <- "grey"
E(graphY1W)[weight <= 2.75 & weight >= 2.5 ]$width <- 3
E(graphY1W)[weight >= -2.0 & weight < -1.75 ]$color <- "black" 
E(graphY1W)[weight >= -2.0 & weight < -1.75 ]$width <- 7
E(graphY1W)[weight >= -1.75 & weight <= -1.5 ]$color <- "black"
E(graphY1W)[weight >= -1.75 & weight <= -1.5 ]$width <- 3
E(graphY1W)[weight <= 2.0 & weight >  1.75 ]$color <- "grey"
E(graphY1W)[weight <= 2.0 & weight >  1.75 ]$width <- 7
E(graphY1W)[weight <= 1.75 & weight >= 1.5 ]$color <- "grey"
E(graphY1W)[weight <= 1.75 & weight >= 1.5 ]$width <- 3
V(graphY1W)$color <-  ifelse(V(graphY1W)$name=="Pn", "grey",
                      ifelse(V(graphY1W)$name=="gs", "grey",
                      ifelse(V(graphY1W)$name=="Pn_amb.Pn_sat", "grey",
                      ifelse(V(graphY1W)$name=="WUE", "grey",
                      ifelse(V(graphY1W)$name=="WP", "grey",
                      ifelse(V(graphY1W)$name=="TL", "grey95",
                      ifelse(V(graphY1W)$name=="FMDM", "grey95",
                      ifelse(V(graphY1W)$name=="Chl", "grey45",
                      ifelse(V(graphY1W)$name=="Chlab", "grey45",
                      ifelse(V(graphY1W)$name=="Car", "grey45",
                      ifelse(V(graphY1W)$name=="MP", "white",
                      ifelse(V(graphY1W)$name=="LMF", "white",
                      ifelse(V(graphY1W)$name=="SRR", "white",
                      ifelse(V(graphY1W)$name=="SLAP", "grey95",
                      ifelse(V(graphY1W)$name=="AP", "grey95",
                      ifelse(V(graphY1W)$name=="NAR", "white",
                      ifelse(V(graphY1W)$name=="RGR", "white","white")))))))))))))))))
plot(graphY1W,layout=layout.fruchterman.reingold, vertex.color=V(graphY1W)$color,
     vertex.label.color="black", vertex.shape="circle",vertex.size=10, vertex.label.cex=0.9,
     asp=0.5, frame=FALSE)

1 个答案:

答案 0 :(得分:3)

我不确定你做错了什么,但这是怎么做的。

library(igraph)
g <- graph.ring(10)
E(g)$lty <- 1:5
plot(g)

graph plot

你可能没有任何虚线边缘,这就是他们不能出现的原因。顺便说一句。 weight <= -4.0 & weight < -3.75是多余的,也许您需要weight > -3.75