A GPU-accelerated node-link diagram for large gene/protein interaction
networks. Nodes and edges are rendered with WebGL (via sigma v3 over a
graphology graph) so tens of thousands of elements stay interactive. When
node coordinates are not supplied, a bounded ForceAtlas2 layout positions the
nodes in the browser; otherwise the supplied x/y are used. Categorical
node groups take the theme's palette: Okabe-Ito (colour-blind safe) for up
to 8 groups, and a 20-colour palette past 8. Hovering a node
highlights it and its neighbors; zoom and pan use sigma's camera. When the
nodes have groups, a legend of the groups sits in a gutter on the right. In
a Shiny app, clicking a node sets input$<id>_selected to the clicked node
id, and clicking the empty canvas clears it.
Usage
network(
nodes,
edges,
layout = c("forceatlas2", "precomputed"),
iterations = 200,
default_node_color = "#7c8598",
default_edge_color = NULL,
label_threshold = 4,
default_node_size = 4,
directed = FALSE,
palette = NULL,
show_legend = TRUE,
bounded_view = TRUE,
theme = NULL,
width = NULL,
height = NULL,
element_id = NULL
)Arguments
- nodes
A data frame of nodes. Must contain an
idcolumn (coerced to character). Optional columns:x,y(precomputed coordinates),size(node radius in px),group(categorical, mapped to a palette color) andlabel(display name; defaults toid). A factorgroupfixes the order of the legend and the colours to its levels, and keeps unused levels in the legend, the waydrop = FALSEdoes in ggplot2; other groups go in the order the nodes have them.- edges
A data frame of edges with columns
sourceandtargetholding node ids, an optional numericweightcolumn, and an optionalcolorcolumn giving a per-edge color (elsedefault_edge_color). Edge width follows the absolute weight, scaled over the weights in the graph to 1.7-6 px, and the layout pulls nodes together by the absolute weight, so a negative weight (as in a correlation network) counts by its size.- layout
Either
"forceatlas2"(run a layout when coordinates are missing) or"precomputed"(require and usex/yfromnodes).- iterations
Number of ForceAtlas2 iterations (bounded internally).
- default_node_color
Fallback node color for nodes without a
group.- default_edge_color
Colour of an edge with no colour of its own.
NULL(the default) takes the theme background with a fifth of the theme foreground mixed in, so edges stay quieter than the nodes and the labels on a light and on a dark background.- label_threshold
Minimum node size (px) on screen for its label to render. At the default of 4, nodes of the default size are labelled at the starting zoom, and the renderer's label grid keeps the labels from crowding. Where the letters of two labels would run into each other, the label of the smaller node is left out.
- default_node_size
Node radius (px) used when
sizeis absent.- directed
Draw the graph as directed, with arrowheads. When
TRUE,A -> BandB -> Aare kept as distinct edges; whenFALSE(the default) the graph is undirected and a reciprocal pair collapses to one line.- palette
Optional character vector of hex colors overriding the default categorical palette used for node groups.
- show_legend
Draw a legend of the node groups, in a gutter on the right, when
nodeshas agroupcolumn.- bounded_view
Keep pan and zoom inside the graph: zooming out stops at the view that shows all of it, a pan stops at its edges, and zooming in stops at 1/10,000 of its range.
FALSElets the view move freely. Either way a double-click, or the reset button in the top right corner of the plot, goes back to the view of the whole graph.- theme
Optional named list of theme overrides (colors, fonts, ...) merged over the component defaults in the browser.
NULLuses the default theme. Itspaddingentry is the space in px between the edge of the widget and the plot: one number for every side, or numbers namedtop,right,bottomandleftin a list or vector, a side left out keeping the default of 10. Itscategoricalentry is an ordered palette: the colours go to the node groups in order, and names on the colours are not matched to them. A colour given as a CSS custom property, such asbackground = "var(--bg)", takes the value the page gives it when the plot draws. After the page changes the property, what the plot drew on a canvas keeps the old colour until it draws again: resize the widget or render it again.- width, height
Widget dimensions (any valid CSS size).
- element_id
Optional explicit DOM id.
Examples
set.seed(1)
nodes <- data.frame(
id = paste0("N", 1:60),
group = sample(c("A", "B", "C"), 60, replace = TRUE)
)
edges <- data.frame(
source = paste0("N", sample(1:60, 120, replace = TRUE)),
target = paste0("N", sample(1:60, 120, replace = TRUE))
)
network(nodes, edges)