Examples pulled from Network Visualization with R, Polnet 2016 by Katya Ognyanova.

suppressPackageStartupMessages({
  library(RColorBrewer)
  library(readr)
  library(dplyr)
  library('visNetwork') 
})

Here use visNetwork package. visNetwork enables setting graphic properties as node or edge attributes by adding them as data columns before calling visNetwork(). See available options with:

?visNodes
?visEdges

For more information, see:

?visOptions # available options 
?visLayout  # available layouts
?visGroups  # using node groups
?visLegend  # adding a legend

Basic visNetwork with Dataset 1

nodes <- 
   read_csv(system.file("extdata", "ShinyLogicNodes.csv", package = "qtl2shiny")) %>%
  mutate(id = node) %>%
  select(-node)
links <- 
   read_csv(system.file("extdata", "ShinyLogicEdges.csv", package = "qtl2shiny")) %>%
  mutate(from = source,
         to = target) %>%
  select(-source,-target)

Open dataset 1 with visNetwork.

visNetwork(nodes, links, width="100%", height="400px", main="Network!")

Changing some node parameters:

nodes$shape <- "dot"  
nodes$shadow <- TRUE # Nodes will drop shadow
nodes$title <- nodes$id # Text on click
nodes$label <- nodes$logic # Node label
#nodes$size <- nodes$audience.size # Node size
nodes$borderWidth <- 2 # Node border width

nodes$color.background <- c("skyblue", "tomato", "slategrey", "gold")[factor(nodes$logic)]
nodes$color.border <- "black"
nodes$color.highlight.background <- "orange"
nodes$color.highlight.border <- "darkred"

visNetwork(nodes, links)

Set up hover option.

nodes$color.hover.background <- "green"
visNetwork(nodes, links) %>%
 visInteraction(hover = TRUE)

Change visual properties of edges.

#links$width <- 1 + links$weight/8 # line width
links$color <- "gray"    # line color  
links$arrows <- "middle" # arrows: 'from', 'to', or 'middle'
links$smooth <- FALSE    # should the edges be curved?
links$shadow <- FALSE    # edge shadow

visNetwork(nodes, links)

visNetwork offers a number of other options in the visOptions() function. For instance, highlight all neighbors of a selected node (highlightNearest), or add a drop-down menu to select groups of nodes (selectedBy). The groups are based on data columns such as type label.

## nodes must be a data frame, not tbl_df.
visNetwork(as.data.frame(nodes), links) %>%
  visOptions(highlightNearest = TRUE, 
             selectedBy = "type.label")


byandell/qtl2shiny documentation built on June 11, 2025, 4:54 a.m.