How to plot networks with bipartite

Since version 3.0 of bipartite the plotweb and plotweb2 functions are deprecated and replaced by the S3 functions plot.bipartite_web and plot.bipartite_webarray.

So creating a plot for the Safariland web is now this simple:

plot(Safariland)

Text size and spacing

The “magic” behind these well-scaled, out-of-the-box plots lies in the text_size and spacing arguments. By default, text_size is set to "auto" and spacing to 0.3. This means that, unless specified otherwise, the spacing between the boxes on each side will occupy 30% of the total available space in that axis, and the size of the text labels is automatically scaled so that they cannot overlap.

To better understand the spacing parameter, let us have a look at a simple layout consisting of 2 columns and 1 row. To help visualize the structure, we enable the x- and y-axes by setting plot_axes = TRUE. In the resulting plot, the row1 box spans exactly 0.7 units in width, while the col1 and col2 boxes are each 0.35 units wide. As described above, this leaves 0.3 units—or 30% of the total width—as spacing between the elements.

web <- bipartite_web(matrix(c(50, 50), ncol = 2))
plot(web, plot_axes = TRUE)

If we explicitly set the spacing value to 0.1, we can see that the width of the lower box increases to 0.9 units, and the size of the upper boxes increases to 0.45 units each.

plot(web, spacing = 0.1, plot_axes = TRUE)

If you prefer larger labels, you can set text_size to a value greater than 1 to increase their size. Conversely, if you want smaller labels, set text_size to a value between 0 and 1.

plot(web, spacing = 0.1, text_size = 2)

If you have found a text_size that you like and simply want to scale the plot automatically so that labels do not overlap, you can also set spacing = "auto".

plot(Safariland, spacing = "auto", text_size = 0.75)

The option text_size = "auto" is designed to automatically reduce the text size only when necessary. If all labels fit within the plot at the default size of 1, that size will be used. Otherwise, the text size is scaled down just enough to ensure everything fits without overlap.

Switching to a horizontal plot

If you prefer your labels to be horizontal, another option is to rotate the whole plot by 90° altogether. This can be accomplished by setting horizontal = TRUE.

web <- bipartite_web(genweb(5, 6))
plot(web, horizontal = TRUE)

Now we are able to clearly read every label in the natural reading direction and still interpret the plotted interactions.

Sorting the web

By default, the species in the plot are arranged according to the rows and columns of the given web matrix. The first row/column is therefore plotted at the left of the plot (or at the top for horizontal plots). To arrange the plot in a specific order, simply provide that order when calling plotweb. Like in the example below, in which we reverse the row order for the plotweb call.

plot(web[rev(rownames(web)), ], horizontal = TRUE)

Now the plant species are plotted in reverse order.

However a built-in call to sortweb is also possible with the argument sorting. The options are the same as for sortweb. So dec orders the species in decreasing row/column totals, inc orders by increasing totals, and ca performs a correspondence analysis which might reduce link crossings in the plot.

plot(Safariland, horizontal = TRUE, sorting = "dec")

plot(Safariland, horizontal = TRUE, sorting = "inc")

plot(Safariland, horizontal = TRUE, sorting = "ca")

Adding independent abundances

In some cases, in addition to the recorded interactions, data on the actual abundance of some or all species may also be available. In such cases, it can be useful to modify the plot so that the boxes reflect species abundances, while the link widths continue to represent interaction preferences. This can be done using the two arguments higher_abundances and lower_abundances.

These take a named vector each—for the higher trophic level species (columns) and the lower trophic level species (rows).

Since we don’t have actual recorded abundances, we will generate some randomly for demonstration purposes.

load("Safariland.web.RData")
Safariland.web
## <bipartite_web object>
## Dimensions: 9 x 27 
## -------------------------------------------------- 
## Higher species attributes:
##                              Family       Order
## Policana albopilosa      Colletidae Hymenoptera
## Bombus dahlbomii             Apidae Hymenoptera
## Ruizantheda mutabilis    Halictidae Hymenoptera
## Trichophthalma amoena Nemestrinidae     Diptera
## Syrphus octomaculatus     Syrphidae     Diptera
##  [ reached 'max' / getOption("max.print") -- omitted 22 rows ]
## Lower species attributes:
##                                 Family       Life.form Origin
## Aristotelia chilensis    Eleocarpaceae Understory tree Native
## Alstroemeria aurea    Alstroemeriaceae            Herb Native
## Schinus patagonicus      Anacardiaceae Understory tree Native
##  [ reached 'max' / getOption("max.print") -- omitted 6 rows ]
## Meta attributes:
## $type
## [1] "pollination"
## 
## -------------------------------------------------- 
## Matrix:
##                          Policana albopilosa Bombus dahlbomii
## Aristotelia chilensis                    673                0
##                          Ruizantheda mutabilis Trichophthalma amoena
## Aristotelia chilensis                      110                     0
##                          Syrphus octomaculatus Manuelia gayi
## Aristotelia chilensis                        0             0
##                          Allograpta.Toxomerus Trichophthalma jaffueli Phthiria
## Aristotelia chilensis                       0                       0        0
##                          Platycheirus1
## Aristotelia chilensis                4
##  [ reached 'max' / getOption("max.print") -- omitted 8 rows and 17 columns ]
lower_attributes(Safariland.web)$l_abuns <- sample(0:100,
                                                   nrow(Safariland.web),
                                                   replace = TRUE)
options(max.print = 100)
lower_attributes(Safariland.web)
##                                    Family       Life.form Origin l_abuns
## Aristotelia chilensis       Eleocarpaceae Understory tree Native      68
## Alstroemeria aurea       Alstroemeriaceae            Herb Native      55
## Schinus patagonicus         Anacardiaceae Understory tree Native      48
## Berberis darwinii           Berberidaceae           Shrub Native      74
## Rosa eglanteria                  Rosaceae           Shrub Exotic      76
## Cynanchum diemii           Asclepiadaceae            Vine Native      28
## Ribes magellanicum          Saxifragaceae           Shrub Native      26
## Mutisia decurrens              Asteraceae            Vine Native      24
## Calceolaria crenatiflora Scrophulariaceae            Herb Native      65

Now we have a named vector with a random abundance for each row (lower-level species / plants) in the Safariland web, as required by the function. When we call the function with this argument, we get the following result.

plot(Safariland.web, sorting = "ca", horizontal = TRUE,
     lower_abundances = "l_abuns")

We now have abundances for the pollinator species that were randomly drawn from a uniform distribution between 0 and 100. As we can see the box sizes on the right are more homogeneous.

We can also see that some of the links are increasing or decreasing in size from the left to the right.

Adding additional abundances

Sometimes, not all individuals of a species are involved in observed interactions—such as unvisited plants, or unparasitized hosts.

For these cases, specifying independent abundances is not sufficient. However, additional abundances can be defined using the arguments add_higher_abundances and add_lower_abundances. Simply set these to the number of individuals not observed in the interaction matrix. These individuals will then be drawn as an extension to the main box.

Both arguments, just as their independent counterparts, take a named vector of all species as input. Set add_lower_abundances To demonstrate we take the same abundances as above.

plot(Safariland.web, sorting = "ca", horizontal = TRUE,
     add_lower_abundances = "l_abuns")

As you can see the additional abundances are now plotted as red extra boxes on top of the original boxes.

Switching to absolute scaling

In many cases having independent abundances will lead to the total abundances on one side being larger than on the other side. However by default the plotweb function will make both sides use the maximum available space.

If you are interested in the absolute relations between both sides you need to set the option scaling = "absolute". To demonstrate the difference we will generate uniformly random abundance values for the pollinator that are much larger than the actual values in the web.

higher_attributes(Safariland.web)$h_abuns <- sample(0:1000,
                                                    ncol(Safariland.web),
                                                    replace = TRUE)
plot(Safariland.web, sorting = "ca", horizontal = TRUE,
     higher_abundances = "h_abuns", scaling = "relative")

plot(Safariland.web, sorting = "ca", horizontal = TRUE,
     higher_abundances = "h_abuns", scaling = "absolute")

In the same way as for independent abundances a plot with additional abundances is by default scaled in a relative manner. That means the boxes on both sides use as much space as possible. However,

plot(Safariland.web, sorting = "ca", horizontal = TRUE,
     add_lower_abundances = "l_abuns", scaling = "absolute")

Aesthetics

Now that we’ve covered the basics of plotting bipartite networks, let’s explore how to customize the plots to better match your desired aesthetics.

Cursive species labels

The options higher_italic and lower_italic make the higher trophic level and lower trophic level species labels respectively cursive.

plot(Safariland, sorting = "ca", horizontal = TRUE, curved_links = TRUE,
     higher_italic = TRUE, lower_italic = TRUE)

If you take a closer look at the labels on the left (the pollinators or higher trophic species), you will notice that not all of them are full species names. For instance, Phthiria is just a genus name. So, to make things a bit more interesting in the example below, we use the higher_labels option to italicize only those labels that contain two words separated by a space—usually the species names. Feel free to copy the code snippet below if you ever want to do the same.

higher_labels <- colnames(Safariland)
names(higher_labels) <- higher_labels
species_name_selector <- lengths(strsplit(higher_labels, " ")) == 2
higher_species_names <- higher_labels[species_name_selector]
higher_labels[higher_species_names] <- lapply(higher_species_names,
                                              function(x) bquote(italic(.(x))))

plot(Safariland, sorting = "ca", horizontal = TRUE, curved_links = TRUE,
     higher_labels = higher_labels, lower_italic = TRUE)

Coloring all nodes

With the arguments higher_color and lower_color the colors of the boxes can be changed. For example setting these to a single color value changes the color of all boxes on each side.

plot(Safariland, sorting = "ca", horizontal = TRUE, curved_links = TRUE,
     higher_labels = higher_labels, lower_italic = TRUE,
     higher_color = "orange", lower_color = "darkgreen")

As you can see all boxes on the left (higher or columns) are orange and all boxes on the right (lower or rows) are dark green.

plot(Safariland, sorting = "ca", horizontal = TRUE, curved_links = TRUE,
     higher_labels = higher_labels, lower_italic = TRUE,
     lower_color = rainbow(nrow(Safariland)))

Coloring based on attributes

However oftentimes we want to highlight just a few species. For the example below we are especially interested in the interaction of the plant species Alstroemeria aurea. So in the first step we generate a vector for all lower species and fill it with the value "black". In the next step we change the value only for the considered species to "orange".

plot(Safariland.web, sorting = "ca", horizontal = TRUE, curved_links = TRUE,
     higher_labels = higher_labels, lower_italic = TRUE,
     lower_color = c("Native" = "black", "Exotic" = "orange"),
     lower_color_attr = "Origin")

As you can see now only the box of Rosa eglanteria is orange.

Coloring additional abundances

Of course the color of the additional abundance boxes can be specified as well via the options higher_add_color and lower_add_color.

plot(Safariland.web, sorting = "ca", horizontal = TRUE,
     add_lower_abundances = "l_abuns", curved_links = TRUE,
     higher_labels = higher_labels, higher_color = "grey50",
     lower_italic = TRUE,
     lower_color_attr = "Origin",
     lower_color = c("Native" = "black", "Exotic" = "orange"),
     link_color = "lower",
     lower_add_color = c("Native" = "black", "Exotic" = "darkorange"))

Plotting 2 webs

The S3 method of plot for bipartite_webarray stacks stacks two networks to create a tripartite graph. By default the first two networks of the array are plotted. However arbitrary arbitrary networks can be selected with the options web1 and web2.

webs <- bipartite_webarray(Safariland, vazarr)
plot(webs)

plot(webs, web1 = "vazarr", web2 = "Safariland", horizontal = TRUE,
     higher_color = "red", higher_border = FALSE)