【问题标题】:Set edge colours based on edge relationships ggnet2, igraph r根据边缘关系设置边缘颜色 ggnet2, igraph r
【发布时间】:2018-06-27 04:40:28
【问题描述】:

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

有什么建议吗?

【问题讨论】:

  • get.edge.attribute(max_spann_tree3 , "color") 返回什么?听起来你那里有 NA 值。
  • max_spann_tree3max_spann_tree2 只是同一张图的副本吗?或者max_spann_tree3 是如何创建的?
  • 当你掌握 get.edge.attribute(max_spann_tree3 , "color") 时返回 NA。我使用max_spann_tree3&lt;-asNetwork(max_spann_tree2)max_spann_tree3 %v% "he1" &lt;- he1 只是为了设置节点颜色,如您在我的代码中看到的那样
  • 在这种情况下,它应该具有与原始图相同的边权重,因此您应该能够调用 E(max_spann_tree3)$color&lt;-ifelse(E(max_spann_tree3)$weight &gt; 0.5, "black", "red") 而无需参考原始图。
  • 感谢您的努力,但我部分解决了这个ifelse(E(mygraph)$correlation&gt;=0, 'red','blue'),成立于stackoverflow.com/questions/35315988/…。有什么建议使用更多颜色来表示边缘值吗?我应该使用另一个函数而不是 ifelse,对吗?

标签: r igraph ggnetwork


【解决方案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")

【讨论】:

  • 谢谢,我可以使用E(tree_graph)$color 进行彩色打印。唯一的问题是 R 返回我:Error in ggnet2(max_spann_tree3, size = nlav1, size.cut = 4, label.size = 4, : incorrect edge.color value
  • 确保在将 cut 结果分配给 E(tree_graph)$color 之前将其转换为类字符
猜你喜欢
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多