Skip to contents

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 id column (coerced to character). Optional columns: x, y (precomputed coordinates), size (node radius in px), group (categorical, mapped to a palette color) and label (display name; defaults to id). A factor group fixes the order of the legend and the colours to its levels, and keeps unused levels in the legend, the way drop = FALSE does in ggplot2; other groups go in the order the nodes have them.

edges

A data frame of edges with columns source and target holding node ids, an optional numeric weight column, and an optional color column giving a per-edge color (else default_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 use x/y from nodes).

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 size is absent.

directed

Draw the graph as directed, with arrowheads. When TRUE, A -> B and B -> A are kept as distinct edges; when FALSE (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 nodes has a group column.

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. FALSE lets 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. NULL uses the default theme. Its padding entry is the space in px between the edge of the widget and the plot: one number for every side, or numbers named top, right, bottom and left in a list or vector, a side left out keeping the default of 10. Its categorical entry 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 as background = "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.

Value

An htmlwidget object.

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)