Learn R Programming

ggsketch (version 2.0.0)

force_layout: Force-directed graph layout (Fruchterman-Reingold)

Description

A pure-R implementation of the Fruchterman-Reingold force-directed layout, so geom_sketch_node() / geom_sketch_edge() can place a network with no external graph dependency. Repulsive forces push every node apart; attractive forces pull edge-connected nodes together; a cooling schedule settles the system. Coordinates are returned rescaled to roughly [-1, 1] on both axes.

Usage

force_layout(edges, n_nodes = NULL, niter = 500L, seed = NULL)

Value

A data frame with columns x and y, one row per node in index order.

Arguments

edges

A two-column matrix or data frame of 1-based integer node indices, one row per edge (from, to). May have zero rows (an edgeless graph, laid out on a circle).

n_nodes

Number of nodes. Defaults to the largest index in edges (so isolated high-index nodes need this set explicitly).

niter

Number of iterations. Default 500.

seed

Integer seed for the initial placement. NULL uses getOption("ggsketch.seed", 1L). The layout is otherwise deterministic.

Examples

Run this code
# A small ring of five nodes
e <- cbind(1:5, c(2:5, 1))
force_layout(e, seed = 1L)

Run the code above in your browser using DataLab