| Title: | Plots for Bipartite Networks in R |
|---|---|
| Description: | Provides functions to plot bipartite ecological networks using 'ggplot2' graphics, including interaction webs, node-link layouts, and matrix representations. Species interaction data in the form of adjacency matrices, or 'network' and 'igraph' graph objects, can be plotted directly. |
| Authors: | Pedro Jordano [aut, cre] |
| Maintainer: | Pedro Jordano <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.3 |
| Built: | 2026-09-20 16:34:06 UTC |
| Source: | https://github.com/pedroj/bipartite_plots |
mymat is the adjacency matrix, to get the dimensions from. net is a network object corresponding to mymat. The object net can be initialized with function bip_netinit.R
bip_binplot( mymat, net, usearrows = FALSE, mode = "kamadakawai", displaylabels = TRUE, label.cex = 0.6, vertex.cex = 1, edge.lty = 0.7 )bip_binplot( mymat, net, usearrows = FALSE, mode = "kamadakawai", displaylabels = TRUE, label.cex = 0.6, vertex.cex = 1, edge.lty = 0.7 )
mymat |
the adjacency matrix. Input can be a data.frame or a matrix, representing the adjacency matrix. |
net |
a network object. |
usearrows |
a ntework object. |
mode |
mde of the network object. |
displaylabels |
whether or not display the node labels. |
label.cex |
size of label fonts. |
vertex.cex |
size of nodes. |
edge.lty |
line type for the edges. |
Plot of a network object corresponding to a binary adjacency matrix (qualitative, presence-absence, data.
A plot object.
mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) net <- bip_init_network(mat) bip_binplot(mat, net)mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) net <- bip_init_network(mat) bip_binplot(mat, net)
Function to compute scaled edge weights for plotting.
bip_edgewt(M, x = 30)bip_edgewt(M, x = 30)
M |
the adjacency matrix. Input can be a data.frame or a matrix, representing the adjacency matrix. |
x |
a scaling parameter. Just tune it (x parameter) to adequate values for the specific dataset. |
A vector of scaled edge weights for plotting.
mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) # One scaled weight per non-zero interaction; the largest equals x. wt <- bip_edgewt(mat, x = 30) summary(wt)mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) # One scaled weight per non-zero interaction; the largest equals x. wt <- bip_edgewt(mat, x = 30) summary(wt)
Plotting bipartite networks from adjacency matrix of a two-mode network.
bip_ggnet( net, mat, mode = "fruchtermanreingold", size = 9, palette = c(A = "grey", P = "gold"), color = "mode", label.size = 3, label = FALSE, shape = "mode", edge.label = NULL, layout.exp = 0, mode1 = "P", mode2 = "A" )bip_ggnet( net, mat, mode = "fruchtermanreingold", size = 9, palette = c(A = "grey", P = "gold"), color = "mode", label.size = 3, label = FALSE, shape = "mode", edge.label = NULL, layout.exp = 0, mode1 = "P", mode2 = "A" )
net |
the network object. Either a two-mode object of class
|
mat |
the adjacency matrix. Input can be a data.frame or a matrix, representing the adjacency matrix.
Optional: if not supplied it is extracted from |
mode |
the type of energy-minimization algorithm to plot the network. By default it's mode= "fruchtermanreingold". |
size |
Specify size for nodes. |
palette |
Define the color palette to use. |
color |
Node color. |
label.size |
Size for node text labels. |
label |
Whether or not add node labels. |
shape |
Geometric shape to be used for nodes. |
edge.label |
Whether or not add edge labels. |
layout.exp |
Layout information. |
mode1 |
name for the upper mode (matrix rows), used when |
mode2 |
name for the lower mode (matrix columns), used when |
Inputs are a net object and its corresponding adjacency matrix. It uses the adjacency matrix to compute scaled weighted edges with function edgewt. Part of the code from Francois Briatte, using package ggnet. DATE: 15Jul2013. Updated to ggnet 11/11/2016. ggnet2 automatically detects two-mode graphs from their bipartite network attribute. To simplify the plotting of each mode, it understands arguments of the form [alpha, color, shape, size] = "mode", which will mark the primary mode as "actor" and the secondary mode as "event". #—————————————————————————- Detect and color the mode To use the mode of the nodes as the basis for their colors, all the user has to do is to pass the color = "mode" argument, and then to style the "actor" and "event" values. #—————————————————————————
A ggplot object.
mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) net <- bip_init_network(mat) bip_ggnet(net, mat) # igraph input works too, and the matrix can be omitted g <- bip_init_igraph(mat) bip_ggnet(g)mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) net <- bip_init_network(mat) bip_ggnet(net, mat) # igraph input works too, and the matrix can be omitted g <- bip_init_igraph(mat) bip_ggnet(g)
Plotting bipartite networks from adjacency matrix of a two-mode network.
bip_igplot(mymat, g)bip_igplot(mymat, g)
mymat |
the adjacency matrix. Input can be a data.frame or a matrix, representing the adjacency matrix. |
g |
the graph object |
Plotting bipartite networks from adjacency weighted matrix of two-mode network. A modified version of the plotting, with package igraph. Requires input of the weighted "mymmat" matrix and its graph object, "g". #—————————————————————————
An igraph graph object.
mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) g <- bip_init_igraph(mat) bip_igplot(mat, g)mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) g <- bip_init_igraph(mat) bip_igplot(mat, g)
Function to initialize a bipartite web for library igraph (type igraph).
bip_init_igraph(mat)bip_init_igraph(mat)
mat |
the adjacency matrix. Input can be a data.frame or a matrix, representing the adjacency matrix. |
Function to initialize a bipartite web for library igraph (type igraph), starting from a weighted adjacency matrix. Create the graph object (a bipartite, weighted graph) for igraph.
A graph object.
mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) g <- bip_init_igraph(mat) gmat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) g <- bip_init_igraph(mat) g
Function to initialize a bipartite web for library network (type network).
bip_init_network(mymat, mode1 = "P", mode2 = "A")bip_init_network(mymat, mode1 = "P", mode2 = "A")
mymat |
the adjacency matrix. Input can be a data.frame or a matrix, representing the adjacency matrix. |
mode1 |
name for the upper mode. |
mode2 |
name for the lower mode. |
Function to initialize a bipartite web for library network (type network),
A network object.
mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) net <- bip_init_network(mat) netmat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) net <- bip_init_network(mat) net
Function to plot a bipartite weighted network.
bip_qtplot(mat)bip_qtplot(mat)
mat |
the adjacency matrix. Input can be a data.frame or a matrix, representing the adjacency matrix. |
Plotting a bipartite network from the weighted (quantitative) adjacency matrix of a two-mode network. Uses objects of type network and a traditional plot.
A plot object.
mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) bip_qtplot(mat)mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) bip_qtplot(mat)
Function to plot layout coordinates for railway network plot.
bip_railway(mymat, nodesize = 9, label = FALSE)bip_railway(mymat, nodesize = 9, label = FALSE)
mymat |
a matrix whose rows contain the x,y coordinates of the vertices of d. |
nodesize |
size of nodes. |
label |
should nodes be labelled? |
Plotting a bipartite network from the weighted (quantitative) adjacency matrix of a two-mode network.
# Code for separately nudging and rotating labels of modes. geom_text(aes(label= network.vertex.names(test.net)), angle<- ifelse(network.vertex.names(test.net)[1:dim(mymat)[1]], 0, 45), nudge_x<- ifelse(network.vertex.names(test.net)[1:dim(mymat)[1]], 0.5, -0.5))
A plot object.
mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) bip_railway(mat, label = TRUE)mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) bip_railway(mat, label = TRUE)
Bipartite networks
The ggbipart package includes a series of R functions aimed to plot bipartite networks within the ggplot2 environment. The library relies heavily on code developed by Francois Briatte for the ggnet library. Bipartite networks are a special type of network where nodes are of two distinct types or sets, so that connections (links) only exist among nodes of the different sets. As in other types of network, bipartite structures can be binary (only the presence/absence of the links is mapped) or quantitative (weighted), where the links can have variable importance or weight. To plot, we start with an adjacency or incidence matrix. Using matrices that illustrate ecological interactions among species, such as the mutualisttic interactions of animal pollinators and plant flowers. The two sets (modes) of these bipartite networks are animals (e.g., pollinators) and plants species. From any adjacency matrix we can get a network object or an igraph object for plotting and analysis.
Installation.
'# devtools::install_github("pedroj/bipartite_plots")'
'# library(ggbipart)'
Suggested references:
Pocock, M.J.O., Evans, D.M., Fontaine, C., Harvey, M., Julliard, R., McLaughlin, O., Silvertown, J., Tamaddoni-Nezhad, A., White, P.C.L. and Bohan, D.A. (2016) The visualisation of ecological networks, and their use as a tool for engagement, advocacy and management. Advances in Ecological Research, 1st ed, pp. 41-85. Elsevier Ltd.
Bascompte, J., Jordano, P. (2014) Mutualistic Networks. Princeton University Press, Princeton, NJ.
Pedro Jordano
Useful links:
Report bugs at https://github.com/pedroj/bipartite_plots/issues
Function to vectorze an adjacency matrix.
vectorize(mat)vectorize(mat)
mat |
the adjacency matrix. |
Like in STACK: Turn table (C) into (D): The input matrix should be read with row.names=1 option and a blank entry at the upper left corner.
C a b c d A 3 2 . . B . . 1 1
D A a 3 A b 2 A c . A d . B a . B b . B c 1 B d 1 #————————————————————-
A vector object.
# A weighted plant-frugivore adjacency matrix shipped with the package. mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) edges <- vectorize(mat) head(edges)# A weighted plant-frugivore adjacency matrix shipped with the package. mat <- as.matrix(read.csv( system.file("extdata", "sdw02_adj_fru.csv", package = "ggbipart"), comment.char = "#", row.names = 1, check.names = FALSE)) edges <- vectorize(mat) head(edges)