R/art_studio.R

Defines functions mapper_object_to_igraph

Documented in mapper_object_to_igraph

###########################################################################
# ART STUDIO
# graph visualization
###########################################################################

# igraph ------------------------------------------------------------------

#' make igraph
#'
#' @param mapperobject mapper object generated by mappeR
#'
#' @return an igraph object
#' @export
#' @examples
#' data = data.frame(x = sapply(1:100, function(x) cos(x)), y = sapply(1:100, function(x) sin(x)))
#'
#' projy = data$y
#'
#' cover = create_width_balanced_cover(min(projy), max(projy), 10, 25)
#'
#' mapperobj = create_1D_mapper_object(data, dist(data), data$y, cover)
#'
#' mapper_object_to_igraph(mapperobj)
mapper_object_to_igraph <- function(mapperobject) {
  vertices = mapperobject[[1]]
  edges = mapperobject[[2]]

  # deal with edgeless graphs
  if (length(edges$source) <= 1 & any(edges$source == "")) {
      mappergraph = graph_from_data_frame(d = data.frame(id = rownames(vertices), id = rownames(vertices)),
                                          vertices = vertices)
      mappergraph = delete_edges(mappergraph, E(mappergraph))
  } else {
    mappergraph = graph_from_data_frame(d = edges,
                                        directed = FALSE,
                                        vertices = vertices)
  }

  # don't label nodes
  mappergraph = set_vertex_attr(mappergraph, "label", value = NA)

  # set node size to square root of cluster size
  mappergraph = set_vertex_attr(mappergraph, "size", value = sqrt(vertices$cluster_size))

  # color nodes if binning
  if ("bin" %in% colnames(vertices)) {
    num_bins = max(vertices$bin)
    colfunc = colorRampPalette(c("blue", "gold", "red"))
    mappergraph = set_vertex_attr(mappergraph,
                                  "color",
                                  value = sapply(vertices$bin, function(x)
                                    colfunc(num_bins)[x]))
  }

  return(mappergraph)
}

Try the mappeR package in your browser

Any scripts or data that you put into this service are public.

mappeR documentation built on April 3, 2025, 6:19 p.m.