简体   繁体   English

绘制仅边缘值高于igraph或ggnet2中阈值的网络

[英]plot a network with only edge values above a threshold in igraph or ggnet2

I need to graphically represent a network from a symmetric matrix nxn, composed by edge values that go from 0 to 1 (like 0.1, 0.22, 0.54,etc). 我需要用对称矩阵nxn图形化地表示一个网络,该矩阵由从0到1(例如0.1、0.22、0.54等)的边值组成。 I would like to represent only the strongest edge connections, say above 0.6. 我只想表示最强的边缘连接,例如0.6以上。

I share my code: 我分享我的代码:

m=as.matrix(matrix3)
g <- graph_from_adjacency_matrix(m, mode = "upper", weighted = T, diag = F)
new_graph <- induced.subgraph(m, E(m)[E(m)$weight %in% c(E(m)$weight > 0.6 )])
ggnet2(new_graph)

It does not work. 这是行不通的。 Any suggestion to plot only connections above the 0.6 threshold? 有建议只绘制高于0.6阈值的连接吗?

library(igraph)

your data 您的数据

x <- read.table(header=T, stringsAsFactors=FALSE, text='id1 id2 id3 id4 id5 id6 id7 id8 id9 id10    id11
id1 1   0.473684    0.578947    0.368421    0.438596    0.438596    0.175439    0.403509    0.403509    0.245614    0.175439
id2 0.473684    1   0.44898 0.236842    0.347826    0.347826    0.157895    0.384615    0.36    0.236842    0.210526
id3 0.578947    0.44898 1   0.22449 0.469388    0.510204    0.244898    0.403846    0.58    0.204082    0.22449
id4 0.368421    0.236842    0.22449 1   0.26087 0.217391    0.24    0.211538    0.24    0.32    0.24
id5 0.438596    0.347826    0.469388    0.26087 1   0.73913 0.282609    0.576923    0.3 0.282609    0.043478
id6 0.438596    0.347826    0.510204    0.217391    0.73913 1   0.304348    0.653846    0.36    0.23913 0.086957
id7 0.175439    0.157895    0.244898    0.24    0.282609    0.304348    1   0.25    0.16    0.217391    0.052632
id8 0.403509    0.384615    0.403846    0.211538    0.576923    0.653846    0.25    1   0.442308    0.211538    0.173077
id9 0.403509    0.36    0.58    0.24    0.3 0.36    0.16    0.442308    1   0.22    0.34
id10    0.245614    0.236842    0.204082    0.32    0.282609    0.23913 0.217391    0.211538    0.22    1   0.217391
id11    0.175439    0.210526    0.22449 0.24    0.043478    0.086957    0.052632    0.173077    0.34    0.217391    0.12')

Transform into matrix 转换成矩阵

y <- as.matrix(x)

Set up graph object 设置图形对象

g <- graph_from_adjacency_matrix(y, mode = "upper", weighted = T, diag = F)

Delete edges below 0.6 删除低于0.6的边

g2 <- delete.edges(g, which(E(g)$weight <0.6))

Plot your graph with edges above 0.6 用高于0.6的边绘图

plot(g2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM