1

I have a transport network I have represented in igraph, and for each station I have a number of boardings. I'm wondering how I can accumulate the boardings at each node from the end of the graph into the centre (Central Station)?

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) 

I found this data.tree solution, but would like to do it in igraph.

Many thanks,

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
jimbo
  • 176
  • 6

1 Answers1

1

Surely there are many ways to do that, here's one:

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 

Rather than using cumsum somewhere, I used distances with mode = "out" to find which stations ultimately lead to which stations (giving finite values in the distance matrix). Given that, it becomes to sum all the corresponding boardings.

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102