【问题标题】:How can I add geographic distance matrix to an igraph network?如何将地理距离矩阵添加到 igraph 网络?
【发布时间】:2022-04-16 01:00:56
【问题描述】:

我有一个由 169 个节点(社区)组成的 igraph 网络。我添加了一些顶点属性使用: V(g)$attribute。我将研究节点(社区)之间地理距离的影响。我已经根据经度和纬度计算了社区之间的距离:

head(df)
neighbourhood     lon       lat
      1         41.47141  -81.75226
      2         41.47562  -81.74670
      3         41.47608  -81.73900
      .            .          .
      .            .          .

# Calculate the geographic distance matrix using  distm() from geosphere

dis.matrix <- (distm(cbind(df$lon, df$lat)))

# The result is a geographic matrix of 169x169 (distance in meters)

现在,我的问题是如何将这些距离添加到网络 (g) 中,以便使用指数随机图模型 ergm() 研究距离对链接节点的影响。

【问题讨论】:

    标签: r igraph sna


    【解决方案1】:

    我想你正在寻找这个。

    library(igraph)
    library(geosphere)
    
    
    df <- data.frame(neighbourhood = c(1, 2, 3),
                     lon           = c(41.47141, 41.47562, 41.47608),
                     lat           = c(-81.75226, -81.74670, -81.73900))
    
    distance.matrix <- distm(subset(df, select = c(lon, lat)))
    
    my.graph <- graph_from_adjacency_matrix(distance.matrix, mode = "undirected", weighted = TRUE)
    
    # At attributes to the vertices.
    V(my.graph)$name <- df$neighbourhood
    V(my.graph)$lon  <- df$lon
    V(my.graph)$lat  <- df$lat
    
    # Remove duplicated edges.
    my.graph <- simplify(my.graph)
    
    # The distances are saved in the edges in the variable weight.
    E(my.graph)$weight
    
    plot(my.graph, edge.label = E(my.graph)$weight)
    

    HTH!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-10
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2017-05-05
      • 2018-07-20
      • 1970-01-01
      相关资源
      最近更新 更多