Skip to contents

Lays out any undirected graph with ForceAtlas2, for example an SNN graph or a graph from another package. Edge weights come from the weight edge attribute; without one, every edge weighs 1. Self-loops are dropped. Only the optimisation parameters in fa2_params are used; the graph and initialisation ones do not apply here.

Usage

forceatlas2_from_graph(
  graph,
  init = NULL,
  fa2_params = params_fa2(),
  seed = 42L,
  .verbose = TRUE
)

Arguments

graph

An undirected igraph object without multi-edges. Use igraph::as_undirected(mode = "collapse") or igraph::simplify() to get there. If present, the weight edge attribute needs to be finite and non-negative.

init

Optional numerical matrix of dimensions vcount(graph) x 2. The initial layout, e.g. a UMAP of the same samples. If NULL, a random layout is used. Defaults to NULL.

fa2_params

Named list. ForceAtlas2 parameters, see params_fa2().

seed

Integer. Random seed for reproducibility. Defaults to 42L.

.verbose

Logical or integer. Controls verbosity. TRUE or 1L prints timed stages and epochs every 50, 2L adds parameters and the kNN internals, FALSE or 0L is silent. Defaults to TRUE.

Value

A numerical matrix with dimensions vcount(graph) x 2 containing the ForceAtlas2 layout, rows in vertex order.

References

Jacomy, et al., PLoS ONE, 2014

Examples

if (requireNamespace("igraph", quietly = TRUE)) {
  g <- igraph::sample_gnp(200L, 0.05)
  emb <- forceatlas2_from_graph(g, .verbose = FALSE)
  dim(emb)
}
#> [1] 200   2