Visualizing plant-pollinator networks with R

The igraph package for R has some nice features for visualizing networks.

install.package(igraph)
library(igraph)

You can download a copy of the example data set here. Copy the matrix to your clipboard and then read in the data. Rows are plant species, columns are pollinators, and values are relative visitation rates.

net = read.table("clipboard", header=T, row.names=1)

First convert the network to an incidence matrix and then plot it.

i_net = graph_from_incidence_matrix(net, weight=T)
plot(i_net)

Figure1

You can change the layout to a bipartite graph or circular.

plot(i_net, layout=layout.bipartite)
plot(i_net, layout=layout.circle)

Figure2

Enter help(layout) to see a bunch of other layouts. Changing the vertex (V) and edge (E) attributes can make these plots look a bit nicer.

V(i_net)$frame.color = "white"
V(i_net)$label = NA
plot(i_net,layout=layout.circle)

Figure3

We can change the size of the vertices to reflect things like each species’ relative abundance or degree centrality.

#For degree centrality:
deg = centr_degree(i_net, mode="all")
V(i_net)$size = 5*sqrt(deg$res)
plot(i_net, layout=layout.circle)

Figure4

You might have to play around with transformations to get the vertex size right. 5 * the square root of the degree values is shown here. You can also change the color of vertices to highlight plants and pollinators.

polcol = rep("gray50",dim(net)[2])
plantcol = rep("black",dim(net)[1])
clrs = rbind(as.matrix(plantcol),as.matrix(polcol))
V(i_net)$color = clrs
plot(i_net,layout=layout.circle)

Figure5

Next we can change the edge widths to reflect the weight of the interaction.

E(i_net)$width = E(i_net)$weight/15
plot(i_net,layout=layout.circle)

Figure6

Again, you might have to re-scale the weight values or you could get something that looks like this:

Figure7

Vertex colors can also be changed to reflect different species that belong to the same module.

#Identify modules for plants and pollinators
i_net.com = fastgreedy.community(i_net)
plantmod = i_net.com$membership[1:dim(net)[1]]
pollmod = i_net.com$membership[(dim(net)[1]+1):(length(i_net.com$membership))]

#Re-order the network matrix rows and columns by module ID
onet = net[order(plantmod),] 
onet = t(t(onet)[order(pollmod),])
i_net = graph_from_incidence_matrix(onet, weight=T)

#Add a color palette
install.packages("wesanderson")
library(wesanderson)
com.mem = rbind(as.matrix(sort(plantmod)),as.matrix(sort(pollmod)))
colrs = wes_palette(name="Darjeeling", 
               n=max(i_net.com$mem),
               type="continuous")

#Specify vertex and edge parameters
deg = centr_degree(i_net, mode="all")
V(i_net)$size=5*sqrt(deg$res)
V(i_net)$frame.color="white"
V(i_net)$color=colrs[com.mem]
V(i_net)$label=NA 
E(i_net)$width=E(i_net)$weight/10

plot(i_net, layout=layout.circle)

Figure8