R/create_example_graph.R

#' Create example graph from fictional exhibition data
#'
#' @return a tbl_graph object which is a wrapper around an igraph object
#' @export
#'
#' @examples
#' g <- create_example_graph()
create_example_graph <- function(){

  # create example data
  df <- data_frame(
    exh_place_id =  c("1",  "1",  "1",  "2",  "2",  "3",  "3", "4"),
    exhibition_id = c("1-1","1-1","1-2","2-1","2-1","3-1","3-1","4-1"),
    artist_id =     c("A",  "B",  "C",  "A",  "B",  "A",  "C", "C"),
    exh_start_Y =   c(2000, 2000, 2001, 1995, 1995, 2002, 2002, 2001)
  )

  # create lists of exhibitions hosted by exhibition places
  e1 <- df %>% rename(from = exh_place_id, exh_start_Y_from = exh_start_Y, exhibition_id_from = exhibition_id)
  e2 <- df %>% rename(to = exh_place_id, exh_start_Y_to = exh_start_Y, exhibition_id_to = exhibition_id)

  # connect exhibition places by artists shown in exhibitions
  edges.raw <- inner_join(e1, e2, by = "artist_id") %>%
    # columns need to be ordered for creating an igraph object
    select(from, to, everything())

  # only keep edges connecting exhibition places in chronological order
  edges <- edges.raw %>% filter(exh_start_Y_to > exh_start_Y_from)

  # create vertices
  vertices <- data_frame(name = c("1", "2", "3", "4"),
                         type = c("gallery", "gallery", "museum", "gallery"),
                         first_exh_yr = c(2000, 1995, 2002, 2001),
                         last_exh_yr = c(2001, 1995, 2002, 2001))

  graph <- edges %>%
    # create igraph object
    igraph::graph_from_data_frame(vertices = vertices) %>%

    # use tbl_graph wrapper from tidygraph for convenient analysis
    tidygraph::as_tbl_graph()

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