Title: | Network Visualization Using 'sigma.js' |
---|---|
Description: | Interactive visualizations of graphs created with the 'igraph' package using a 'htmlwidgets' wrapper for the 'sigma.js' network visualization v2.4.0 <https://www.sigmajs.org/>, enabling to display several thousands of nodes. While several 'R' packages have been developed to interface 'sigma.js', all were developed for v1.x.x and none have migrated to v2.4.0 nor are they planning to. This package builds upon the 'sigmaNet' package, and users familiar with it will recognize the similar design approach. Two extensions have been added to the classic 'sigma.js' visualizations by overriding the underlying 'JavaScript' code, enabling to draw a frame around node labels, and to display labels on multiple lines by parsing line breaks. Other additional functionalities that did not require overriding 'sigma.js' code include toggling node visibility when clicked using a node attribute and highlighting specific edges. 'sigma.js' is currently preparing a stable release v3.0.0, and this package plans to update to it when it is available. |
Authors: | Thomas Charlon [aut, cre] , CELEHS [aut] (<https://celehs.hms.harvard.edu>), PARSE Health [aut] (<https://parse-health.org>) |
Maintainer: | Thomas Charlon <[email protected]> |
License: | GPL-3 |
Version: | 1.1.0 |
Built: | 2024-11-22 19:29:05 UTC |
Source: | https://gitlab.com/thomaschln/sgraph |
Pipe an object forward into a function or call expression and update the 'lhs' object with the resulting value. Magrittr imported function, see details and examples in the magrittr package.
lhs |
An object which serves both as the initial value and as target. |
rhs |
a function call using the magrittr semantics. |
None, used to update the value of lhs.
Pipe an object forward into a function or call expression. Magrittr imported function, see details and examples in the magrittr package.
lhs |
A value or the magrittr placeholder. |
rhs |
A function call using the magrittr semantics. |
Result of rhs applied to lhs, see details in magrittr package.
Expose the names in 'lhs' to the 'rhs' expression. Magrittr imported function, see details and examples in the magrittr package.
lhs |
A list, environment, or a data.frame. |
rhs |
An expression where the names in lhs is available. |
Result of rhs applied to one or several names of lhs.
Modify the edge colors of a sgraph object by providing a single color. Also works with a vector of correct size.
add_edge_color( sigma_obj, one_color = NULL, color_attr = NULL, color_palette = "Set2" )
add_edge_color( sigma_obj, one_color = NULL, color_attr = NULL, color_palette = "Set2" )
sigma_obj |
sgraph object |
one_color |
A single color to color all of the nodes (hex format) |
color_attr |
The name of an edge attribute |
color_palette |
Name of RColorBrewer palette to use |
sgraph with modified edge colors
library(igraph) library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) %>% add_edge_color(one_color = "#ccc") sig
library(igraph) library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) %>% add_edge_color(one_color = "#ccc") sig
Modify the edge size of a sgraph by providing a single size
add_edge_size(sigma_obj, one_size = NULL)
add_edge_size(sigma_obj, one_size = NULL)
sigma_obj |
sgraph object |
one_size |
A single size to use for all edges |
sgraph with modified edge sizes
library(igraph) library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) %>% add_edge_size(one_size = 5) sig
library(igraph) library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) %>% add_edge_size(one_size = 5) sig
Modify the edge zIndex
add_edge_zindex(sigma_obj, zindex)
add_edge_zindex(sigma_obj, zindex)
sigma_obj |
sgraph object |
zindex |
Zindex value, larger is drawn above. |
sgraph
library(igraph) library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) %>% add_edge_zindex(zindex = 2) sig
library(igraph) library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) %>% add_edge_zindex(zindex = 2) sig
Modify the node attributes of an existing igraph object by providing a dataframe
add_igraph_info(igraph, df_nodes, fields = names(df_nodes))
add_igraph_info(igraph, df_nodes, fields = names(df_nodes))
igraph |
Igraph object to modify |
df_nodes |
Data frame to add to nodes |
fields |
Columns of df_nodes to add. First must be the node identifier. |
A sgraph object with modified node labels
library(igraph) library(sgraph) data(lesMis) df_nodes = cbind.data.frame(name = igraph::vertex_attr(lesMis, 'label'), log10_degree = degree(lesMis)) igraph = add_igraph_info(lesMis, df_nodes) sig <- sigma_from_igraph(lesMis) %>% add_node_size(size_vector = 'log10_degree')
library(igraph) library(sgraph) data(lesMis) df_nodes = cbind.data.frame(name = igraph::vertex_attr(lesMis, 'label'), log10_degree = degree(lesMis)) igraph = add_igraph_info(lesMis, df_nodes) sig <- sigma_from_igraph(lesMis) %>% add_node_size(size_vector = 'log10_degree')
Add a listener to report data from a 'sgraph' object in 'Shiny' back to the R session.
add_listener(sigma_obj, listener)
add_listener(sigma_obj, listener)
sigma_obj |
Sgraph object, created using the sigma_from_igraph function |
listener |
Either "clickNode" to listen to node clicks or "hoverNode" to listen to node hover |
Sgraph object with listener
Modify the node labels of an existing sgraph object by providing an attribute from the initial igraph to use as the labels.
add_node_labels(sigma_obj, label_attr = NULL)
add_node_labels(sigma_obj, label_attr = NULL)
sigma_obj |
sgraph object, returned by sigma_from_igraph function |
label_attr |
Attribute to use to replace node labels |
sgraph object with modified node labels
library(igraph) library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) %>% add_node_labels(label_attr = 'label') sig
library(igraph) library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) %>% add_node_labels(label_attr = 'label') sig
Modify the node size of an existing sgraph object by providing either: (1) A single size to use for all nodes; (2) a vector of node sizes; or (3) a metric to use to scale the nodes.
add_node_size( sigma_obj, min_size = 1, max_size = 3, one_size = NULL, size_vector = NULL )
add_node_size( sigma_obj, min_size = 1, max_size = 3, one_size = NULL, size_vector = NULL )
sigma_obj |
sgraph object, returned by sigma_from_igraph function |
min_size |
Minimum node size on the graph (for scaling) |
max_size |
Maximum node size on the graph (for scaling) |
one_size |
A single size to use for all nodes |
size_vector |
An optional vector with the sizes for each node |
A sgraph object with modified node sizes
library(igraph) library(sgraph) data(lesMis) layout <- layout_nicely(lesMis) # one size for all nodes sig <- sigma_from_igraph(igraph = lesMis, layout = layout) %>% add_node_size(one_size = 3) sig # using a vector custom_size <- log10(degree(lesMis)) sig <- sigma_from_igraph(igraph = lesMis, layout = layout) %>% add_node_size(size_vector = custom_size) sig
library(igraph) library(sgraph) data(lesMis) layout <- layout_nicely(lesMis) # one size for all nodes sig <- sigma_from_igraph(igraph = lesMis, layout = layout) %>% add_node_size(one_size = 3) sig # using a vector custom_size <- log10(degree(lesMis)) sig <- sigma_from_igraph(igraph = lesMis, layout = layout) %>% add_node_size(size_vector = custom_size) sig
Apply spring weights (revert weights). Can add weak links to selected nodes, but best without.
convert_to_spring_weights(df_links, selected_nodes = NULL)
convert_to_spring_weights(df_links, selected_nodes = NULL)
df_links |
Links data frame of a sgraph object |
selected_nodes |
Nodes identifiers to be used for the selection. |
Links data frame of a sgraph object
Build a color map
get_color_map(colors, palette = RColorBrewer::brewer.pal(8, "Dark2"))
get_color_map(colors, palette = RColorBrewer::brewer.pal(8, "Dark2"))
colors |
Groups that will be assigned to colors |
palette |
Palette to use, typically a RColorBrewer palette |
Data frame mapping group names to colors
Get the legend for a sgraph network
get_legend(colors_map, clusters)
get_legend(colors_map, clusters)
colors_map |
Color mapping to use, typically built by 'get_color_map' |
clusters |
Group names |
Ggplot object
Using a selection of nodes, highlight edges linking to nodes that are connected to several nodes from the selection. Differentiate multiple connected and fully connected (all selected nodes). Use a maximum number of connected nodes to use lighter colored edges (default 20).
highlight_multiple_connected( df_links, selected_nodes, n_max = 20, dark_cols = c("#ddd", "#444", "#444"), light_cols = c("#efefef", "#ddd", "#bbb") )
highlight_multiple_connected( df_links, selected_nodes, n_max = 20, dark_cols = c("#ddd", "#444", "#444"), light_cols = c("#efefef", "#ddd", "#bbb") )
df_links |
Links data frame of a sgraph object |
selected_nodes |
Nodes identifiers to be used for the selection. |
n_max |
Maximum number of connected nodes, to use either lighter or darker color sets for edges (default 20). |
dark_cols |
Three hex values for colors to use with n_max. |
light_cols |
Three hex values for colors to use with n_max. |
Links data frame of a sgraph object
Example function to build a graph list object (list of nodes and links data frames) from a kgraph object (embeddings with cosine similarity cut-off based on random null concept pairs and known related concept pairs)
kgraph_to_lgraph(l_fit_embeds)
kgraph_to_lgraph(l_fit_embeds)
l_fit_embeds |
kgraph object: embeddings with cosine similarity cut-off based on random null concept pairs and known related concept pairs |
graph list object: list of nodes and links data frames
Build an igraph object from a graph list object (list of nodes and links data frames)
l_graph_to_igraph(l_graph)
l_graph_to_igraph(l_graph)
l_graph |
graph list object: list of nodes and links data frames |
igraph object
A graph where the nodes are characters in "Les Miserables" and the edges are times that the characters appeared together in the novel.
lesMis
lesMis
An igraph object with 77 nodes and 254 edges
numeric id of nodes
character label (names) of nodes
numeric weight of the edges (number of co-appearances)
D. E. Knuth, The Stanford GraphBase: A Platform for Combinatorial Computing, Addison-Wesley, Reading, MA (1993)
The sgraph R package extends the sigma.js library to enable multiline labels. The Javascript functions will start new lines on line breaks ('\n') and this function enables to easily format the details of nodes by showing the value of the relationship (edge weights) it has with other nodes. See the Shiny examples for use cases.
multiline_labels( df_nodes, display_val_str = "\nP-value: ", replace_codes = TRUE, label_str = "Label: ", group_str = "Group: " )
multiline_labels( df_nodes, display_val_str = "\nP-value: ", replace_codes = TRUE, label_str = "Label: ", group_str = "Group: " )
df_nodes |
Nodes data frame of a sgraph object |
display_val_str |
String that will be prepended to each edge weight |
replace_codes |
Should the label of the node replace the id ? |
label_str |
String that will pe prepended to the node label |
group_str |
String that will pe prepended to the node group |
sgraph object with modified node hidden atribute
Render a sgraph visualization in Shiny
renderSgraph(expr, env = parent.frame(), quoted = FALSE)
renderSgraph(expr, env = parent.frame(), quoted = FALSE)
expr |
An expression that creates a sgraph visualization |
env |
Defaults to parent.frame() (cf. Shiny docs) |
quoted |
Defaults to FALSE (cf. Shiny docs) |
Htmlwidgets render object
First try to linearize the weights with the best logarithmic and polynomial, then perform exponential scaling and set upper and lower bounds.
scale_graph( weights, exp_scale = exp(1), upper_bound_mult = 25, lower_bound_const = 5 )
scale_graph( weights, exp_scale = exp(1), upper_bound_mult = 25, lower_bound_const = 5 )
weights |
Either nodes or links weights vector |
exp_scale |
Scale for exponential transform |
upper_bound_mult |
Constant to multiply weights by after scaling. Use to set an upper bound for weights. |
lower_bound_const |
Constant to set a lower bound for weights. All weights below will be set to lower bound. |
Weights vector
Wrapper function to build a sigma.js visualization of an igraph object and color it using a 'clusters' attribute. It calls the sigma_from_igraph function, and manages labels, node sizes, color mapping, layouts, and optionally arrows. The clusters attribute can be also be disabled to just use the wrapper to manage the other attributes.
sgraph_clusters( igraph, color_map = NULL, label = "name", clusters = TRUE, arrows = FALSE, node_size = NULL, layout = igraph::layout_with_fr(igraph), ... )
sgraph_clusters( igraph, color_map = NULL, label = "name", clusters = TRUE, arrows = FALSE, node_size = NULL, layout = igraph::layout_with_fr(igraph), ... )
igraph |
Igraph object |
color_map |
Use a color mapping to select colors (enables to link the graph with other plots) as returned by the get_color_map function. Leave NULL for automatic colors (default). |
label |
Name of the igraph attribute to use as labels |
clusters |
Whether or not to use a column named clusters to color the nodes. |
arrows |
Whether or not to display arrows on directed edges. |
node_size |
Passed to add_node_size function, either a numeric or an attribute name. Default is NULL. |
layout |
Output of an igraph layout (default: layout_with_fr) |
... |
Passed to sigma_from_igraph |
Htmlwidget object
Create a UI element for a sgraph visualization in Shiny
sgraphOutput(outputId, width = "100%", height = "400px")
sgraphOutput(outputId, width = "100%", height = "400px")
outputId |
ID of the UI element |
width |
Width of the UI element |
height |
Height of the UI element |
Htmlwidgets output object
Basic sigma.js visualization of an igraph object, with pipeable syntax.
sigma_from_igraph( igraph, layout = NULL, label_color = "#fff", width = "100%", height = "400px", elementId = NULL, label_grid_cell_size = 200 )
sigma_from_igraph( igraph, layout = NULL, label_color = "#fff", width = "100%", height = "400px", elementId = NULL, label_grid_cell_size = 200 )
igraph |
Igraph object |
layout |
Output of an igraph layout (default: layout_nicely) |
label_color |
Hex color for labels |
width |
Width of the output graph (default: fit container) |
height |
Height of the output graph (default: fit container) |
elementId |
Do not specify, used by the htmlwidgets package |
label_grid_cell_size |
Sigma.js corresponding parameter. Roughly goes from 1 to 5000, the smaller the more labels displayed. |
Htmlwidget object, meant to be called directly to render a default visualization, or passed to other functions to change attributes (colors, sizes, interactivity, etc.).
library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) sig
library(sgraph) data(lesMis) sig <- sigma_from_igraph(igraph = lesMis) sig