簡體   English   中英

根據邊緣關系ggnet2,igraph r設置邊緣顏色

[英]Set edge colours based on edge relationships ggnet2, igraph r

我正在用ggnet2繪制圖形。 我想根據邊緣關系的值繪制邊緣。 到目前為止,我只能在邊緣上表示邊緣值,在下面可以看到我的代碼的一部分:

ggnet2(max_spann_tree3 ,size =  nlav1,size.cut = 4, edge.size = 1, edge.color = "grey", edge.label = E(max_spann_tree2)$weight,edge.label.size = 2, color = "he1", color.legend = "industry", palette = "Set3")

這是我在代碼中用於邊緣標簽的值:

edge.label=E(max_spann_tree2)$weight
> edge.label
  [1] 0.4047619 0.3703704 0.5483871 0.4727273 0.5510204 0.6078431 0.5490196 0.6451613 0.7254902 0.4489796
 [11] 0.6000000 0.4074074 0.5714286 0.6973684 0.8181818 0.8701299 0.6578947 0.4210526 0.5128205 0.4909091
 [21] 0.6037736 0.3793103 0.4166667 0.3750000 0.5000000 0.3000000 0.5660377 0.5263158 0.5000000 0.4634146

我想繪制同一張圖,但要根據包含在0.2-0.9范圍內的邊緣關系設置色標,而不必在邊緣標簽中表示它們。 我嘗試了這個: https : //briatte.github.io/ggnet/#edge-size-and-color ,沒有成功。 這是我的嘗試:

>set.edge.attribute(max_spann_tree3 , "color", ifelse(max_spann_tree3 %e%"E(max_spann_tree2)$weight"> 0.5, "black"&& max_spann_tree3 %e%"E(max_spann_tree2)$weight"< 0.5, "red"))
> ggnet2(max_spann_tree3 ,size =  nlav1,size.cut = 4, edge.size = 1, edge.color = "color", edge.label.size = 2, color = "he1", color.legend = "industry", palette = "Set3")

> Error in if (!is_col(edge.color)) { : 
>       missing value where TRUE/FALSE needed

有什么建議么?

如果要通過權重為圖形邊緣着色,可以使用cut函數及其labels參數將顏色屬性分配給邊緣。

library(igraph)
library(GGally)

#simulate a weighted network
set.seed(1)
tree_graph<-random.graph.game(20, p=.2)
E(tree_graph)$weight<-sample(seq(0,1,0.01), ecount(tree_graph), replace=T)


#add colors to edges according to what bin their weight falls in
E(tree_graph)$color<-as.character(cut(E(tree_graph)$weight, 
                                            breaks=c(0, 0.3, 0.5, 0.7, 1), 
                                            labels=c("yellow", "orange", "red", "black")))

#plot graph, verifying that colors match edge weights
ggnet2(tree_graph, edge.color = "color", edge.label = "weight")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM