R/visualize_path.R

Defines functions visualize_path

Documented in visualize_path

#' Visualize the optimal route
#'
#' Plots the optimal route using ggplot2
#'
#' @param epath igraph.es object containing the route
#' @param graph tbl_graph object with the network used to calculate the
#' route
#'
#' @return ggplot of optimal route within the network
#'
visualize_path <- function(epath, graph){

  library(ggplot2)
  library(dplyr)

  path_graph <- graph %>%
    subgraph.edges(eids = epath %>% unlist()) %>%
    as_tbl_graph()

  ggplot() +
    geom_sf(data = graph %>% activate(edges) %>% as_tibble() %>% st_as_sf(), col = 'darkgrey') +
    geom_sf(data = graph %>% activate(nodes) %>% as_tibble() %>% st_as_sf(), col = 'darkgrey', size = 0.5) +
    geom_sf(data = path_graph %>% activate(edges) %>% as_tibble() %>% st_as_sf(), lwd = 1, col = 'firebrick') #+
    #geom_sf(data = path_graph %>% activate(nodes) %>% filter(nodeID %in% c(from_node, to_node)) %>% as_tibble() %>% st_as_sf(), size = 2)

}
kjetilhaukas/shortestpath documentation built on April 28, 2022, 12:06 a.m.