Skip to contents

Turns an edge list into the two data frames that geom_sketch_node() and geom_sketch_edge() consume, placing nodes with the pure-R force-directed force_layout(). Accepts a plain two-column data frame (extra columns are carried through as edge attributes) or - optionally - an igraph object.

Usage

sketch_graph(edges, nodes = NULL, niter = 500L, seed = NULL)

Arguments

edges

A data frame or matrix whose first two columns are the edge endpoints (any identifiers), or an igraph object. Extra data-frame columns are kept as edge attributes.

nodes

Optional. A character vector of node identifiers, or a data frame whose first column is the identifier (further columns are kept as node attributes). Sets the node universe and ordering; defaults to the identifiers seen in edges.

niter, seed

Passed to force_layout().

Value

A list with two data frames: nodes (name, x, y, plus any node attributes) and edges (from, to, x, y, xend, yend, plus any edge attributes).

Examples

edges <- data.frame(from = c("A", "A", "B"), to = c("B", "C", "C"))
g <- sketch_graph(edges, seed = 1L)
g$nodes
#>   name          x           y
#> 1    A  1.0000000  0.05530784
#> 2    B -1.0000000  1.00000000
#> 3    C -0.9275911 -1.00000000
g$edges
#>   from to  x          y       xend yend
#> 1    A  B  1 0.05530784 -1.0000000    1
#> 2    A  C  1 0.05530784 -0.9275911   -1
#> 3    B  C -1 1.00000000 -0.9275911   -1