R/construct_career_path_network.R

#' Construct network of artist's career path
#'
#' @param .exhplaces a data frame of vertices
#' @param .exhibitions a data frame of artist's exhibition participations
#'
#' @return a tbl_graph object
#' @export
#'
#' @examples
#' graph <- construct_career_path_network(exhplaces, exhibitions)
construct_career_path_network <- function(.edges, .exhplaces,
                                          .remove_isolates = F, .remove_loops = F,
                                          .as_tbl_graph = F){

  # exclude vertices not present in edgelist for igraph
  nodes <- .exhplaces %>%
    dplyr::filter(id %in% .edges$from | id %in% .edges$to)


  graph <- igraph::graph_from_data_frame(.edges,
                                         directed = T,
                                         vertices = nodes)
  if(.remove_loops == T){
    graph <- igraph::simplify(graph, remove.multiple = F, remove.loops = T)
  }

  # removing loops before removing isolates will ensure are only self-referential nodes will be excluded,
  # that is, nodes exhibit artists that do not move to other venues
  if(.remove_isolates == T){
    graph <- igraph::delete.vertices(graph, degree(graph) == 0)
  }

  if(.as_tbl_graph == T){
    graph <- tidygraph::as_tbl_graph()
  }


  return(graph)
}
Framus94/HierarchiesAndCareers documentation built on June 5, 2019, 8:52 a.m.