如何使用igraph使边的权重,颜色和线型依赖于边值?

huwehgph  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(103)

我试图绘制一个网络,显示17种植物性状之间的依赖关系。该网络将用于帮助解释其他一些分析的结果,而不是为依赖关系本身提供方向。
我在R中使用igraph,输入一个17 x17的相关矩阵。矩阵中的相关性被改变,第一位数字代表一个类别。虽然我对R相当陌生,但从某种意义上说,我几乎可以生成我所设想的图形。
但是,我找不到一种方法使边样式依赖于相关性的值。我可以对颜色和权重这样做,但是以依赖于权重的方式添加lty或edge.lty并不能得到所需的图形,尽管代码运行没有错误。我们的想法是使用3种线型,这样介于-4和-3之间的值与介于-3和-2等之间的值具有不同的样式。“组”表示仅在处理x中或仅在y中等存在的相关性。
在这里四处寻找帮助很多次,我没有遇到这个问题,所以我想我应该问一下。
我一直在使用的代码(下面)可能不漂亮,但对于像我这样没有经验的用户来说,它最不起作用:)
我尝试(在其他人中)添加E(graphY 1 W)[weight <= -4.0 & weight < -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)
3b6akqbq

3b6akqbq1#

我不知道你做错了什么,但这里是如何做到这一点。

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

你可能没有任何虚线边缘,这就是为什么他们不显示。顺便说一下,weight <= -4.0 & weight < -3.75是多余的,也许你想要weight > -3.75

相关问题