0

我正在用 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

有什么建议么?

4

1 回答 1

2

如果要按权重为图形边缘着色,可以使用该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")
于 2018-06-26T20:40:28.950 回答