rigraphggnetwork

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). I would like to represent only the strongest edge connections, say above 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?


Solution

  • 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

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

    Plot your graph with edges above 0.6

    plot(g2)