图中节点的累积值

时间:2018-12-02 15:07:30

标签: r sum igraph

我有一个在igraph中代表的运输网络,并且对于每个车站,我都有许多登机口。我想知道如何从图的末端到中心(中央车站)累积每个节点的登机牌?

library(tidyverse)
library(igraph)
library(ggraph)

rm(list=ls())

nodes <- tribble(
          ~name, ~netboardings,
    "Station A",            10,
    "Station B",            20,
    "Station C",            30,
    "Station D",            10,
    "Central Station",      20,
    "Station F",            30,
    "Station G",            50,
    "Station H",            60,
    "Station I",            80
    )

ties <- tribble(
          ~from,         ~to,
    "Station C", "Station B",
    "Station B", "Station A",
    "Station A", "Station D",
    "Station G", "Station A",
    "Station H", "Station I",
    "Station I", "Station F",
    "Station F", "Station D",
    "Station D", "Central Station"
)

g <- 
  ties %>% 
  graph_from_data_frame(directed = TRUE, vertices = nodes)

g %>%
  ggraph() + 
  geom_node_point(aes(size=netboardings),color="blue") +
  geom_edge_link(arrow = arrow(length = unit(2, 'mm'),type = "closed")) +
  geom_node_label(aes(label=name),repel = TRUE) 

我找到了this data.tree solution,但我想在igraph中这样做。

非常感谢,

1 个答案:

答案 0 :(得分:1)

当然有很多方法可以做到这一点,

colSums(is.finite(distances(g, mode = "out")) * nodes$netboardings)
#       Station A       Station B       Station C       Station D Central Station 
#             110              50              30             290             310 
#       Station F       Station G       Station H       Station I 
#             170              50              60             140 

我没有在某个地方使用cumsum,而是将distancesmode = "out"一起使用来找出哪些站点最终导致了哪些站点(在距离矩阵中给出有限的值)。这样,就可以对所有相应的登机牌进行汇总。