Render a network with Sigma.js. Accepts either a square weight / adjacency
matrix (e.g. a correlation matrix) or an edge-list data frame, dispatching on
the type of x. Communities are detected with Louvain and laid out with
ForceAtlas2 by default.
Usage
draw_network(
x,
nodes = NULL,
directed = FALSE,
threshold = NULL,
self_loops = FALSE,
layout = "force",
node_size = 10,
edge_scale = 3,
node_opacity = 0.95,
edge_opacity = 0.4,
show_labels = TRUE,
scale_by_degree = TRUE,
color_by_group = FALSE,
resolution = 1,
blend_edges = FALSE,
palette = palette_colors(rtemis_colors),
node_color = rtemis_colors[["teal"]],
positive_color = rtemis_colors[["teal"]],
negative_color = "#ff9e1f",
title = NULL,
theme = NULL,
width = NULL,
height = NULL,
element_id = NULL,
filename = NULL
)Arguments
- x
Numeric matrix or data.frame: A square weight / adjacency matrix, or an edge-list data frame with
source/targetcolumns (and optionalweight,sign). For a matrix, dimnames supply node ids and[i, j]is the edge weight (zero /NAentries produce no edge); the absolute value drives thickness and the sign drives color. For an edge list,sourceandtargetname the endpoints (the first two columns if those names are absent), with optionalweightandsigncolumns.- nodes
Optional Data frame: Node attributes (
id/name,label,value,group). Used only with edge-list input; ignored for matrix input.- directed
Logical: Whether the graph is directed.
- threshold
Optional Numeric [0, Inf): For matrix input, drop edges whose absolute weight is below this value.
- self_loops
Logical: For matrix input, whether to keep diagonal self-edges.
- layout
Character {"force", "circular", "circlepack", "random"}: Layout algorithm.
"force"is ForceAtlas2.- node_size
Numeric [0, Inf): Base node radius in screen pixels.
- edge_scale
Numeric [0, Inf): Multiplier mapping normalized edge weight to stroke width.
- node_opacity
Numeric [0, 1]: Node fill opacity.
- edge_opacity
Numeric [0, 1]: Edge stroke opacity.
- show_labels
Logical: Whether to render node labels.
- scale_by_degree
Logical: Scale each node's radius by its
value(weighted degree); whenFALSEall nodes use the base size.- color_by_group
Logical: Color nodes by detected community (Louvain) instead of a single hue.
- resolution
Numeric [0, Inf): Louvain resolution; higher yields more, smaller communities.
- blend_edges
Logical: Color each edge as the blend of its two endpoint node colors instead of by sign.
- palette
Character: Categorical colors for communities.
- node_color
Character: Single node color used when
color_by_groupisFALSE.- positive_color
Character: Edge color for positive-sign edges.
- negative_color
Character: Edge color for negative-sign edges.
- title
Optional Character: Title shown above the network.
- theme
Optional Theme, list, or
NA: Theme override.NULLenables light/dark auto-detection (matchingdraw()).- width
Optional Character or Numeric: Widget width.
- height
Optional Character or Numeric: Widget height.
- element_id
Optional Character: Explicit element ID.
- filename
Optional Character: Static export of network widgets is not yet supported. Supplying a path raises an error.
Examples
# Correlation matrix
draw_network(cor(mtcars), threshold = 0.5)
# Edge list
edges <- data.frame(
source = c("a", "a", "b"),
target = c("b", "c", "c"),
weight = c(1, 2, 0.5)
)
draw_network(edges)