R/graph_aes.R

Defines functions .finite_adoption_time .diffusion_states .shared_range .shared_levels .shared_aes .infer_line_type .is_concept_lattice .concept_is_labelled .is_hasse_layout .infer_edge_arrows .infer_arrow .drawn_esize .edge_width_range .infer_esize .infer_ecolor_title .has_layers .layer_attribute .infer_ecolor .ecolor_values .infer_ncolor .is_continuous .ncolor_values .mode_labels .infer_nshape .nshape_values .check_aes_length .node_level .pad_for_nodes .widen_expand .node_padding .drawn_nsize .lineage_size_range .default_nsize .infer_nsize .inform_constant_color

# Aesthetic-resolution helpers shared by graphr() (via graph_nodes()/graph_edges())
# and grapht(). Each resolves an argument that may be a literal value (e.g.
# node_size = 6) or the name of a node/tie attribute into the vector or scalar
# actually mapped in the plot.

# An attribute that takes the same value everywhere cannot distinguish anything,
# so it is dropped in favour of plain black. Reported the same way wherever it
# happens, in both graphr() and grapht().
.inform_constant_color <- function(arg, attribute, what) {
  manynet::snet_info(
    "Drawing every {what} black, because {.arg {arg}} was mapped to",
    "{.val {attribute}}, which holds the same value for every {what}.",
    "To colour {what}s differently, map {.arg {arg}} to an attribute that",
    "varies between them.")
}

# Node aesthetics ----

.infer_nsize <- function(g, node_size, layout = NULL) {
  if (!is.null(node_size)) {
    if (is.character(node_size)) {
      out <- manynet::node_attribute(g, node_size)
    } else out <- node_size
    out <- .check_aes_length(out, g, "node_size", manynet::net_nodes(g), "node")
    # A vector of proportions (a centrality score, say) would be invisible at
    # face value, so rescale it into a usable range. A single number is taken
    # at face value: `node_size = 0.5` means 0.5.
    if (length(out) > 1 && all(out <= 1 & out >= 0, na.rm = TRUE)) out <- out * 10
  } else {
    out <- .default_nsize(manynet::net_nodes(g))
    # The default size shrinks with how crowded the plot is, but a multilevel
    # layout draws each level in a plane of its own, so each is only as crowded
    # as itself. Sizing them from the whole network instead would draw the
    # smaller level -- 53 of `fict_marvel`'s 194 nodes -- as if it held all of
    # them, which is where the one-mode structure of such networks is.
    lvl <- .node_level(g, layout)
    if (!is.null(lvl))
      out <- vapply(lvl, function(l) .default_nsize(sum(lvl == l)), numeric(1))
  }
  as.numeric(out)
}

.default_nsize <- function(n) min(20, (250 / n) / 2)

# The sizes a "lineage" draws mapped node sizes between. Other layouts squeeze
# a mapped size into a narrow range, which for the 30 works of a
# historiograph is between 1.7 and 3.3, too close to tell a work cited three
# times from one cited 25 times. A "lineage" leaves room for its nodes along
# each layer, so it draws them by area up to a larger maximum, which still
# shrinks as the network grows.
.lineage_size_range <- function(n) c(2, min(10, max(5, 270 / n)))

# The size each node is drawn at, in the units of `geom_point()`'s `size`.
# That is the size a mapped attribute is scaled to by `scale_size()`, by area,
# for a "lineage". Every other layout is given `.infer_nsize()` as it was, so
# that nothing else changes for them.
.drawn_nsize <- function(g, node_size, layout = NULL) {
  nsize <- .infer_nsize(g, node_size, layout)
  if (!identical(layout, "lineage") || length(unique(nsize)) < 2L) return(nsize)
  rng <- .lineage_size_range(as.numeric(manynet::net_nodes(g)))
  rng[1] + diff(rng) * sqrt(.rescale(nsize))
}

# Room at the panel edge for the nodes drawn there.
#
# A node is drawn at an absolute size, in millimetres, but a scale is expanded
# by a share of its data range. The two never meet, so a node on the edge of
# the layout hangs over the panel edge and is clipped. `ggplot2`'s default
# expansion of 5% is under half the radius of a node in a small network drawn
# on a short panel: `ison_adolescents` draws its 8 nodes about 16mm across, and
# 5% of a 3-inch panel is under 4mm against a radius of nearly 8mm.
#
# The room is therefore taken from the node size rather than left at a
# constant. Dividing by 100 holds the radius of a node on a panel of about
# 4 inches, which is the short side of a figure at the sizes these are drawn
# at, and a large network needs nothing, since its nodes are already small.
.node_padding <- function(nsize) {
  nsize <- suppressWarnings(max(as.numeric(nsize), na.rm = TRUE))
  if (!is.finite(nsize)) return(0.05)
  max(0.05, nsize / 100)
}

# Widens whichever expansion a scale already carries, rather than replacing it.
# A layered or lineage layout sets its own expansion, to leave room for labels
# on one side, and that room must survive.
.widen_expand <- function(expand, mult) {
  if (is.null(expand) || inherits(expand, "waiver"))
    return(ggplot2::expansion(mult = mult))
  if (length(expand) == 2L) expand <- c(expand, expand)
  expand[c(1L, 3L)] <- pmax(expand[c(1L, 3L)], mult)
  expand
}

# Gives the nodes at the edge of the layout room to be drawn whole.
# Called once the layout, the nodes and the labels are all in place, so that a
# scale one of them set is widened rather than replaced; replacing it would
# both warn and drop the room that scale was set to keep.
.pad_for_nodes <- function(p, nsize) {
  mult <- .node_padding(nsize)
  for (axis in c("x", "y")) {
    sc <- p[["scales"]]$get_scales(axis)
    if (is.null(sc)) {
      p <- p + if (axis == "x")
        ggplot2::scale_x_continuous(expand = ggplot2::expansion(mult = mult))
      else ggplot2::scale_y_continuous(expand = ggplot2::expansion(mult = mult))
    } else sc[["expand"]] <- .widen_expand(sc[["expand"]], mult)
  }
  p
}

# The level each node is drawn at by a multilevel layout, or NULL where the
# network is not being drawn that way. Only how the nodes are grouped matters
# here, not which group ends up at which level, so unlike `.infer_level()` in
# R/layout_levels.R there is no need to work out which mode holds the ties
# within itself.
.node_level <- function(g, layout) {
  if (!identical(layout, "levels")) return(NULL)
  if ("lvl" %in% igraph::vertex_attr_names(g))
    return(as.integer(as.factor(igraph::vertex_attr(g, "lvl"))))
  if (!manynet::is_twomode(g)) return(NULL)
  as.integer(manynet::node_is_mode(g)) + 1L
}

# A value mapped to an aesthetic has to be either a single value or one per
# node/tie; ggplot2 would otherwise report the mismatch in terms of its own
# internal data frame ("Aesthetics must be either length 1 or the same as the
# data (8)"), which does not say which argument was wrong.
.check_aes_length <- function(out, g, arg, n, what) {
  len <- length(out)
  if (len == 1L || len == n) return(out)
  manynet::snet_abort(
    "{.arg {arg}} should be a single value or one value for each of the",
    "{n} {what}s in the network, but {len} value{?s} {?was/were} given.")
}

# The categories the node shape shows, or NULL where every node is drawn with
# the same shape. Read as in `.infer_nshape()`.
.nshape_values <- function(g, node_shape) {
  if (!is.null(node_shape)) {
    if (!node_shape %in% manynet::net_node_attributes(g)) return(NULL)
    return(as.factor(as.character(manynet::node_attribute(g, node_shape))))
  }
  if (!is_twomode(g)) return(NULL)
  modes <- .mode_labels(g)
  factor(ifelse(igraph::V(g)$type, modes[2], modes[1]), levels = modes)
}

.infer_nshape <- function(g, node_shape, levels = NULL) {
  if (!is.null(node_shape)) {
    if (node_shape %in% manynet::net_node_attributes(g)) {
      out <- as.factor(as.character(manynet::node_attribute(g, node_shape)))
      if (!is.null(levels)) out <- factor(as.character(out), levels = levels)
    } else out <- node_shape
  } else if (is_twomode(g) & is.null(node_shape)) {
    # igraph convention: type FALSE is the first mode, TRUE the second.
    # A factor rather than a character vector, so that the first mode takes
    # the first shape in the scale (a circle) and the second mode the second
    # (a square). Relying on the labels sorting into that order only held
    # while they were "One" and "Two": mode names need not be alphabetical,
    # and `ison_southern_women`'s "social events" sort before its "women".
    modes <- .mode_labels(g)
    out <- factor(ifelse(igraph::V(g)$type, modes[2], modes[1]), levels = modes)
  } else {
    out <- 21  # Use fillable circle shape (was "circle")
  }
  out
}

# What to call each of a two-mode network's modes in the shape legend. Where
# the network records them, "characters" and "teams" say far more than "One"
# and "Two"; `mode_names()` returns NULL where it does not, and a single name
# for a one-mode network, so both modes have to be there to be used.
.mode_labels <- function(g) {
  modes <- tryCatch(manynet::mode_names(g), error = function(e) NULL)
  if (length(modes) != 2 || anyNA(modes) || any(!nzchar(modes)))
    return(c("One", "Two"))
  as.character(modes)
}

# The categories the node colour shows, before anything is decided about how to
# draw them, or NULL where the colour is one colour rather than a mapping.
# Separated from `.infer_ncolor()` so that `.shared_aes()` can read the
# categories of every network in a `graphs()` list, including those of a network
# that holds only one of them.
.ncolor_values <- function(g, node_color) {
  if (is.null(node_color)) return(NULL)
  if (!node_color %in% manynet::net_node_attributes(g)) return(NULL)
  vals <- manynet::node_attribute(g, node_color)
  if ("node_mark" %in% class(vals))
    return(factor(as.character(vals), levels = c("FALSE", "TRUE")))
  if (.is_continuous(vals)) return(as.numeric(vals))
  as.factor(as.character(vals))
}

# A measure such as coreness or centrality has an order and a distance between
# its values that a set of categories does not, so it is drawn as a gradient
# rather than given one colour for each value it takes. Marks are logical and
# memberships are characters, so neither of them reaches this.
# Two values are a contrast rather than a gradient, and are left to the
# categorical palette, which draws such a pair in the theme's base and
# highlight colours anyway.
.is_continuous <- function(vals) {
  if (!is.numeric(vals) || is.factor(vals)) return(FALSE)
  length(unique(stats::na.omit(as.numeric(vals)))) > 2
}

# `levels` holds the categories that `graphs()` found across all of its panels.
# Where it is given, a network holding only one of them keeps that category
# rather than collapsing to plain black, so that a category is drawn in the same
# colour in every panel.
.infer_ncolor <- function(g, node_color, levels = NULL) {
  vals <- .ncolor_values(g, node_color)
  if (is.null(vals)) return(if (!is.null(node_color)) node_color else ag_ink())
  # A gradient has no categories to hold level for level across the panels of a
  # `graphs()` plot; its panels are held together by a shared range instead.
  if (is.numeric(vals)) return(vals)
  if (!is.null(levels)) return(factor(as.character(vals), levels = levels))
  if (length(unique(vals)) == 1) {
    .inform_constant_color("node_color", node_color, "node")
    return(rep(ag_ink(), manynet::net_nodes(g)))
  }
  vals
}

# Edge aesthetics ----

# The categories the edge colour shows, before anything is decided about how to
# draw them: the attribute the user named, else the layer each tie belongs to,
# else its sign. NULL where the colour is one colour rather than a mapping.
# Separated from `.infer_ecolor()` for the same reason as `.ncolor_values()`.
.ecolor_values <- function(g, edge_color) {
  if (!is.null(edge_color)) {
    if (!edge_color %in% manynet::net_tie_attributes(g)) return(NULL)
    vals <- manynet::tie_attribute(g, edge_color)
    return(if ("tie_mark" %in% class(vals))
      factor(as.character(vals), levels = c("FALSE", "TRUE")) else
        as.factor(as.character(vals)))
  }
  # Which layer a tie belongs to says more about a multiplex network than
  # its sign does, and only some of its ties have a sign to show: a sign is
  # still drawn, as the linetype, but every tie belongs to a layer.
  # Ordered alphabetically, as every other attribute mapped to a colour is.
  # Taking the order from `manynet::layer_names()` instead was tried and
  # dropped: with the two-value highlight palette it decides which layer is
  # drawn in the emphasis colour, and `fict_marvel` names its layers in an
  # order that greys out the very layer the plot is about.
  if (.has_layers(g))
    return(as.factor(as.character(
      manynet::tie_attribute(g, .layer_attribute(g)))))
  # Signed networks that are not layered can still carry a sign on only
  # some of their ties. Treat those (and any NA) as positive, as the
  # linetype does, so that the factor never contains NA, which grid rejects
  # at draw time, and so that colour and linetype agree about which ties
  # are negative.
  if (manynet::is_signed(g)) {
    signs <- as.numeric(manynet::tie_signs(g))
    return(factor(ifelse(is.na(signs) | signs >= 0, "Positive", "Negative"),
                  levels = c("Positive", "Negative")))
  }
  NULL
}

# `levels` is read as in `.infer_ncolor()`.
.infer_ecolor <- function(g, edge_color, levels = NULL){
  vals <- .ecolor_values(g, edge_color)
  if (is.null(vals)) return(if (!is.null(edge_color)) edge_color else ag_ink())
  # A single category across every panel is given no colour scale, so it is
  # drawn in ink, as it would be in a panel of its own.
  if (!is.null(levels) && length(levels) == 1L && is.null(edge_color))
    return(ag_ink())
  if (!is.null(levels)) return(factor(as.character(vals), levels = levels))
  if (length(unique(vals)) == 1) {
    # An attribute the user named is reported when it cannot distinguish
    # anything; a default the package chose itself is not.
    if (!is.null(edge_color)) {
      .inform_constant_color("edge_color", edge_color, "tie")
      return(rep(ag_ink(), manynet::net_ties(g)))
    }
    return(ag_ink())
  }
  vals
}

# Which tie attribute records the layer each tie belongs to, or NA where none
# does. manynet spells this attribute "type" through 2.2.3 and "layer" from
# 2.3.0, and both spellings appear in the networks 2.3.0 ships, so the
# attribute the network carries decides rather than the manynet version.
# `manynet::net_layers()` reads only the "type" spelling from an igraph, and
# so counts one layer for a network whose ties record a "layer", which is why
# the layers are counted here instead.
.layer_attribute <- function(g) {
  atts <- manynet::net_tie_attributes(g)
  out <- intersect(c("type", "layer"), atts)
  if (length(out) == 0) NA_character_ else out[1]
}

# Whether the ties are divided between layers to tell apart. This is not
# `is_multiplex()`, which is TRUE wherever the ties carry a 'layer' or 'type'
# column, even where that column holds only one layer. A
# single test covers both that the layers are recorded per tie -- which naming
# them, as `layer_names()` does, need not imply -- and that there are at least
# two of them to tell apart.
.has_layers <- function(g) {
  att <- .layer_attribute(g)
  if (is.na(att)) return(FALSE)
  length(unique(manynet::tie_attribute(g, att))) > 1
}

# What the edge colour legend is titled. `edge_color` names the attribute when
# the user gave one; otherwise the colour carries whatever the default chose,
# so this is decided here alongside `.infer_ecolor()` rather than separately
# by each caller, which is how the legend came to say "Sign" over colours that
# were showing layers.
.infer_ecolor_title <- function(g, edge_color) {
  if (!is.null(edge_color)) return(edge_color)
  if (.has_layers(g)) return("Layer")
  if (manynet::is_signed(g)) return("Sign")
  "Color"
}

.infer_esize <- function(g, edge_size){
  if (!is.null(edge_size)) {
    if (any(edge_size %in% manynet::net_tie_attributes(g))) {
      # strip measure classes (e.g. tie_measure) so scales can rescale
      out <- as.numeric(manynet::tie_attribute(g, edge_size))
    } else {
      out <- edge_size
    }
  } else if (is.null(edge_size) & manynet::is_weighted(g)) {
    out <- as.numeric(manynet::tie_attribute(g, "weight"))
  } else {
    out <- 0.5
  }
  out
}

# The widths the ties are drawn at, which are not always the values given.
# A single width is drawn as it is. Widths that vary are mapped through a
# scale that draws the least of them at 0.3 and the greatest at 3, across
# `limits` where `graphs()` shares one scale between its panels, and across
# the values themselves otherwise. An arrowhead sized from the values would
# follow the units a weight happens to be recorded in; sized from these, it
# follows the lines on the page.
.edge_width_range <- function() c(0.3, 3)

.drawn_esize <- function(esize, limits = NULL) {
  esize <- esize[!is.na(esize)]
  if (is.null(limits)) {
    if (length(unique(esize)) < 2) return(esize)
    limits <- range(esize)
  }
  to <- .edge_width_range()
  # A scale with nothing to spread draws every tie at the middle of its range.
  if (diff(limits) == 0) return(rep(mean(to), length(esize)))
  to[1] + (pmin(pmax(esize, limits[1]), limits[2]) - limits[1]) /
    diff(limits) * diff(to)
}

# `edge_arrows` arrives resolved by `.infer_edge_arrows()`: FALSE for no
# arrowheads, TRUE for arrowheads that follow the width of the ties, or a
# number for arrowheads of that length in millimetres whatever the width.
.infer_arrow <- function(esize, edge_arrows = TRUE, limits = NULL) {
  if (isFALSE(edge_arrows)) return(NULL)
  # `arrow=` is a fixed layer parameter, not a mappable aesthetic, so the
  # widths of a layer whose ties vary are summarised by their mean to pick one
  # arrowhead size for the whole layer.
  repr <- mean(.drawn_esize(esize, limits))
  # A tie that is not drawn has no arrowhead either, whatever size was asked
  # for: `edge_size = 0` is how the ties are hidden.
  if (is.na(repr) || repr <= 0) return(NULL)
  # 2mm at the default edge width (0.5), scaled proportionally and capped so
  # heavily-weighted edges don't get oversized arrowheads. A length the user
  # gave is theirs, and is not capped.
  len_mm <- if (is.numeric(edge_arrows)) edge_arrows else min(repr / 0.5 * 2, 4)
  ggplot2::arrow(angle = 15, type = "closed", length = ggplot2::unit(len_mm, 'mm'))
}

# Decide what `edge_arrows = NULL` means for this network, and set aside a
# request that the network cannot meet. An arrowhead says which way a tie
# points, so an undirected network has none to draw. That is said only to a
# user who asked to hear everything, since the same `edge_arrows` is often
# given to directed and undirected networks alike, as by `graphs()`, where it
# sizes the arrowheads there are. A concept lattice is
# directed, but is read as a Hasse diagram, in which the direction of every
# tie is already given by which end is drawn higher. That holds only where
# the layout draws it so, which `hasse` says: see `.is_hasse_layout()`.
.infer_edge_arrows <- function(g, edge_arrows, hasse = FALSE) {
  if (!manynet::is_directed(g)) {
    if (!is.null(edge_arrows) && !isFALSE(edge_arrows))
      manynet::snet_minor_info(
        "This network is undirected, so {.arg edge_arrows} is ignored",
        "and no arrowheads are drawn.")
    return(FALSE)
  }
  if (!is.null(edge_arrows)) return(edge_arrows)
  if (hasse && .is_concept_lattice(g)) {
    manynet::snet_info(
      "Drawing this concept lattice without arrowheads,",
      "since each tie points down the page.",
      "Use {.code edge_arrows = TRUE} to draw them.")
    return(FALSE)
  }
  TRUE
}

# Whether the layout draws every tie of a directed acyclic network pointing
# down the page, as a Hasse diagram needs. Only "layered" and "railway" do:
# "lineage" and "ladder" run their layers across the page, and any other
# layout, or coordinates the caller gave, puts no meaning in which end of a
# tie is higher. Nor do layers the caller ranked by an attribute, which need
# not follow the ties.
.is_hasse_layout <- function(layout, manual = FALSE, ...) {
  dots <- list(...)
  ranked <- unlist(dots[intersect(names(dots), c("ranks", "rank"))])
  !manual && is.character(layout) && length(layout) == 1 &&
    layout %in% c("layered", "railway") &&
    all(ranked %in% .rank_methods())
}

# Which concepts of a lattice carry a "reduced" label, or NULL where the
# network is not a lattice that says. Each node of the first set is named at
# the lowest concept that holds it, which is the one with the smallest extent,
# and each node of the second set at the highest concept that holds it, which
# is the one with the smallest intent. This is read from the extents and
# intents themselves rather than from the names, since a node of the original
# network may well be called "C1", as the concepts that name nothing are.
.concept_is_labelled <- function(g) {
  if (!.is_concept_lattice(g)) return(NULL)
  ig <- manynet::as_igraph(g)
  if (!all(c("extent", "intent") %in% igraph::vertex_attr_names(ig)))
    return(NULL)
  first_at <- function(sets) {
    sets <- lapply(sets, as.character)
    size <- lengths(sets)
    members <- unique(unlist(sets))
    vapply(members, function(m) {
      holds <- which(vapply(sets, function(s) m %in% s, logical(1)))
      holds[which.min(size[holds])]
    }, integer(1))
  }
  seq_len(igraph::vcount(ig)) %in%
    c(first_at(igraph::vertex_attr(ig, "extent")),
      first_at(igraph::vertex_attr(ig, "intent")))
}

# `manynet::to_concepts()` records the projection it made in the network's
# information, which is the only thing that tells a concept lattice from any
# other directed acyclic network. The record outlives a later change to the
# network, so the ties are checked too: a lattice that has been made
# undirected, say, is no longer drawn as one.
.is_concept_lattice <- function(g) {
  made <- tryCatch(igraph::graph_attr(manynet::as_igraph(g), "transformations"),
                   error = function(e) NULL)
  if (!is.list(made)) return(FALSE)
  "concept lattice" %in% unlist(made[["projection"]]) &&
    manynet::is_directed(g) && manynet::is_acyclic(g)
}

.infer_line_type <- function(g) {
  if (manynet::is_signed(g)) {
    signs <- as.numeric(manynet::tie_signs(g))
    # Ties without a sign (e.g. non-signed layers of a multiplex network) come
    # back as NA; draw them solid rather than passing NA (an invalid linetype)
    # through to grid. Only genuinely negative ties are dashed.
    out <- ifelse(!is.na(signs) & signs < 0, "dashed", "solid")
    # Collapse to a scalar when every tie is the same so it is treated as a
    # constant layer parameter rather than a per-tie aesthetic.
    if (length(unique(out)) == 1) out <- unique(out)
  } else out <- "solid"
  out
}

# Shared aesthetics across a list of networks ----

# `graphs()` draws each network as a plot of its own and lets `{patchwork}`
# collect the guides. A guide can only be collected when it is identical to the
# one beside it, and a scale takes its limits from the data of its own plot, so
# two networks holding different values of the same attribute produce two
# guides. Worse, the palette and the value each category is drawn in are chosen
# from the categories of that network alone, so the same category can be drawn
# in a different colour in each panel.
#
# This resolves each aesthetic over the whole list, so that every panel is drawn
# and labelled against the same scale. It reads the same helpers the panels
# themselves use, so the categories and the sizes cannot disagree.
#
# An entry is NULL where the aesthetic does not vary across the list, which
# leaves the panel to decide as it does for a single plot.
.shared_aes <- function(netlist, node_color = NULL, node_shape = NULL,
                        node_size = NULL, edge_color = NULL, edge_size = NULL,
                        layout = NULL) {
  nets <- lapply(netlist, function(x)
    tryCatch(manynet::as_tidygraph(x), error = function(e) NULL))
  nets <- Filter(Negate(is.null), nets)
  if (length(nets) < 2) return(NULL)
  gather <- function(f) lapply(nets, function(g)
    tryCatch(f(g), error = function(e) NULL))
  out <- list(
    esize  = .shared_range(gather(function(g) .infer_esize(g, edge_size))),
    nsize  = if (is.null(node_size)) NULL else
      .shared_range(gather(function(g) .infer_nsize(g, node_size, layout))),
    ecolor = .shared_levels(gather(function(g) .ecolor_values(g, edge_color))),
    ncolor = .shared_levels(gather(function(g) .ncolor_values(g, node_color))),
    ncolor_range = .shared_range(gather(function(g) .ncolor_values(g, node_color))),
    nshape = .shared_levels(gather(function(g) .nshape_values(g, node_shape))),
    diffusion = .shared_levels(gather(.diffusion_states)),
    nadopt = .shared_range(gather(.finite_adoption_time)))
  if (all(vapply(out, is.null, logical(1)))) NULL else out
}

# The union of the categories a mapping takes across the list, in the order they
# are first met, or NULL where there is nothing to tell apart.
.shared_levels <- function(vals) {
  vals <- Filter(function(x) is.factor(x) || is.character(x), vals)
  if (!length(vals)) return(NULL)
  levs <- unique(unlist(lapply(vals, function(x)
    if (is.factor(x)) levels(x) else unique(as.character(x)))))
  levs <- levs[!is.na(levs)]
  if (length(levs) < 2) NULL else levs
}

# The range a continuous mapping covers across the list, or NULL where every
# network holds the same single value and so needs no scale to tell them apart.
.shared_range <- function(vals) {
  vals <- unlist(Filter(is.numeric, vals))
  vals <- vals[is.finite(vals)]
  if (length(unique(vals)) < 2) NULL else range(vals)
}

# The categories the two diffusion mappings in R/graph_nodes.R show, read the
# same way there.
.diffusion_states <- function(g) {
  if (!"diffusion" %in% manynet::net_node_attributes(g)) return(NULL)
  states <- c("Susceptible", "Exposed", "Infected", "Recovered")
  out <- .recode_diffusion(manynet::node_attribute(g, "diffusion"))
  factor(out, levels = states[states %in% out])
}

.finite_adoption_time <- function(g) {
  out <- .node_adoption_time(g)
  out[is.finite(out)]
}

Try the autograph package in your browser

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

autograph documentation built on Nov. 16, 2026, 5:08 p.m.