Plot an Ecological Network Organized by Trophic Level using visNetwork
Source:R/plotNetworkVis.R
plot_troph_level_visNet.RdThis function visualizes a food web using visNetwork, where nodes represent species and edges represent interactions.
Nodes are positioned by trophic level on the y-axis, and optionally, community modules on the x-axis.
Unlike plot_troph_level_ggplot() and plot_troph_level_ggraph(), this produces an interactive HTML widget
that supports dragging, zooming, hovering for details, and node search.
Usage
plot_troph_level_visNet(
g,
vertexSizeMin = 8,
vertexSizeMax = 25,
modules = FALSE,
weights = NA,
community_obj = NULL,
node_weights = NA,
module_spacing = 350,
physics = c("x", "full"),
highlight = TRUE,
search = TRUE,
label_size = 16,
width = "100%",
height = "850px"
)Arguments
- g
An
igraphobject representing the ecological network.- vertexSizeMin
Minimum node size (default: 8).
- vertexSizeMax
Maximum node size (default: 25).
- modules
Logical; if
TRUE, nodes are grouped by community modules along the x-axis (default:FALSE).- weights
Edge weights used for community detection when
modules = TRUEandcommunity_objis not supplied (default:NA).- community_obj
Optional pre-computed community detection object (e.g. from
cluster_spinglass()). IfNULLandmodules = TRUE, community detection is run internally viacluster_spinglass().- node_weights
Optional numeric vector used to size nodes. If
NA(default), node degree is used.- module_spacing
Horizontal spacing multiplier between community modules when
modules = TRUE(default: 350). Whenmodules = FALSE, node x-coordinates are instead spread across a wide random range scaled bymodule_spacing * 1.5.- physics
Layout mode, one of
"x"or"full"(default:"x")."x": during the simulation, nodes can move horizontally but their vertical position stays fixed at their trophic level, so the layout settles into trophic bands."full": nodes move freely in both x and y under a force-directed layout (forceAtlas2Based), anchored by a smallcentralGravityto prevent unbounded drift.
In both modes, once stabilization finishes, physics is disabled and every node's
fixed.x/fixed.yis unlocked, so the trophic-level ordering (in"x") or the force-directed layout (in"full") is just the starting point — afterwards any node can be freely dragged in any direction.- highlight
Logical; if
TRUE, hovering over a node highlights its nearest neighbors (default:TRUE).- search
Logical; if
TRUE, adds a node-search dropdown to the widget (default:TRUE).- label_size
Font size, in pixels, used for node labels (default: 16). Apparent label size can differ between
physicsmodes even at the same value, since"full"typically stabilizes into a more spread-out layout that is zoomed further out to fit the view; adjust this parameter if labels look too large or too small for a given mode.- width
Width of the
visNetworkwidget, passed through tovisNetwork::visNetwork()(default:"100%"). Accepts any CSS width string (e.g."600px","100%").- height
Height of the
visNetworkwidget, passed through tovisNetwork::visNetwork()(default:"850px"). Accepts any CSS height string. When embedding in slide frameworks likexaringan, set an explicit pixel height small enough to fit the slide (e.g."400px") — the widget does not shrink to fit its container automatically.
Examples
#
# Example using the Kefi 2015 multiplex dataset shipped with the
# 'multiweb' package (trophic + positive + negative interaction layers)
#
fileName <- system.file("extdata", package = "multiweb")
dn <- list.files(fileName, pattern = "^Kefi2015.*\\.txt$")
g <- readNetwork(dn, fileName, skipColumn = 2)
names(g) <- c("Negative", "Positive", "Trophic")
plot_troph_level_visNet_multi(g, physics = "full")
#> Warning: Can't find 'group' in node data.frame