根据边缘关系ggnet2,igraph r设置边缘颜色

时间:2018-06-20 11:31:30

标签: r igraph ggnetwork

我正在用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范围内的边缘关系设置色标,而不必在边缘标签中表示它们。 我尝试了以下操作:Front end view,但没有成功。 这是我的尝试:

>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

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

如果要通过权重为图形边缘着色,可以使用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")