2

I am conducting ecological network analysis, looking specifically at plant-pollinator mutualisms, using the bipartite package in R.

I have used the visweb() function to create an adjacency matrix using the standard code provided in the bipartite package guide. I want to change the position of the lower level species label as it's currently skewed to the left, but I can't see anything in the package guide which tells you how to do it It currently looks like this.

Could anyone help?

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
acroceph
  • 21
  • 2

1 Answers1

1

Unfortunately, the function visweb() from the bipartite R package does not have any argument to allow the fine adjustment of label's position.

I suggest two solutions, both a bit convoluted:

1 - Transform the plot to gTree object (grid package ideology)

You can transform the plot generated by visweb to a gTree object. A gTree acts as a container list of many grid graphical objects/components ("grobs").

I use here the Safariland data that comes with the bipartite package as an example for the addressed issue:

library(bipartite)
library(gridGraphics)

data(Safariland) # load data
# Creates a gTree object from the "usual" plot generated by `visweb`
my_gTree <- grid.grabExpr(grid.echo(function() visweb(Safariland)))

View the structure of the newly created gTree object. The "grobs" can be indexed as in an usual list, so we can make edits to them. Here I tried to identify the "grobs" that refer to the labels of both axes and edit their positions:

View(my_gTree)
# shift the left axis labels to the right
my_gTree[["children"]][["graphics-plot-1-left-axis-labels-1"]][["x"]] <- unit(0.2, units = "in")
# shift the bottom axis labels upwards
my_gTree[["children"]][["graphics-plot-1-bottom-axis-labels-1"]][["y"]] <- unit(1.5, units = "in")

This is the usual plot, obtained with visweb(Safariland). Note the extra space between the labels and the grid (both left and bottom): enter image description here

Here is the grid version of it with the above edits:

grid.newpage(); grid.draw(my_gTree)

enter image description here

Even more, if you wish, you can save it with ggplot2::ggsave() function:

library(ggplot2)
ggsave(filename = "visweb-grid-version.png", plot = my_gTree)

2 - Adapt/Edit visweb to your needs

If you check the code of the function with View(visweb) and search for the key word labsize for example, you will come to understand that the function has two calls to the function axis; that is, the authors decided to call the axis function to display the labels (species names). You will see that they used something like:

 axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
      mgp = c(0, 0, 0), las = 2, cex.axis = 0.4 * labsize * clratio)

What controls the position of the labels here is the mpg argument. You find more about it with ?par and search for mpg in the help page. If you set mpgto something like c(0, -2, 0) it might solve your issue (need to try out some values until you get it right). The idea is that negative values for the 3rd element will move the labels of the left axis to the right. So edit the code to something like below:

 axis(2, (1:n.prey) - 0.5, labels = ynl, tick = FALSE, 
      mgp = c(0, -2, 0), las = 2, cex.axis = 0.4 * labsize * clratio)

and then save this edited visweb function locally, say visweb.r and source it whenever you need it in your main script with source(visweb.r).

As a side note - this is also an example of the beauty and power of open source - use the work of others and adapt it to your needs. Even more, one can also try to make contributions (or open issues) to the bipartite package on its GitHub repository here so that the community benefits.

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68