【问题标题】:How can i plot an undirected strongly connected graph in R?How can i plot an undirected strongly connected graph in R?
【发布时间】:2022-12-01 21:50:21
【问题描述】:

I want to plot an undirected strongly connected graph like the uploaded picture.

I want to use the adjacency matrix in order to plot it.

I tried the igraph package in R :


data <- matrix(c(0,1,0,0,1,
                 1,0,1,0,0,
                 0,1,0,1,0,
                 0,0,1,0,1,
                 1,0,0,1,0),nrow=5,ncol = 5)
colnames(data) = rownames(data) = LETTERS[1:5]
data

network <- graph_from_adjacency_matrix(data)


plot(network)


But it creates a cycle.

Any help ?

【问题讨论】:

    标签: r graph adjacency-matrix


    【解决方案1】:

    You need only the upper (or lower) triangle of the matrix to be 1, but 0 elsewhere. Make sure you also set mode = "undirected"

    library(igraph)
    
    data <- matrix(0, 5, 5, dimnames = list(LETTERS[1:5], LETTERS[1:5]))
    data[upper.tri(data)] <- 1
    
    data
    #>   A B C D E
    #> A 0 1 1 1 1
    #> B 0 0 1 1 1
    #> C 0 0 0 1 1
    #> D 0 0 0 0 1
    #> E 0 0 0 0 0
    
    network <- graph_from_adjacency_matrix(data, mode = 'undirected')
    
    plot(network)
    

    【讨论】:

      猜你喜欢
      • 2022-11-20
      • 2022-12-01
      • 2022-12-27
      • 2022-12-27
      • 2022-12-01
      • 2022-12-26
      • 2022-12-02
      • 2022-12-27
      • 2022-12-02
      相关资源
      最近更新 更多