We start by defining a cell size. Again, our data are decimal coordinates, and subsequently we will use one degree cells.
cellsize <- 1Note that this may not be the ideal approach for publication. The width of one degree cells decreases towards the poles, and in spatial analyses equal area grid cells are often preferred because they are more comparable. If we want equal area cells we first need to project our data into meters and then use a cellsize in meters (e.g. 100,000 for 100 x 100 km). There are R functions for such spatial projection, but we will simply use one degree cells here.
We make a list of all species and a list of all cells that occur in our dataset, naming the cells after their centres in the format "126.5:-46.5". I assume here that we have the data matrix called 'mydata' from the previous post, with the columns species, lat and long.
allspecies <- unique(mydata$species)We create a matrix of species and cells filled with all zeroes, which means that the species does not occur in the relevant cell. Then we loop through all records to set a species as present in a cell if the coordinates of at least one of its records indicate such presence.
longrounded <- floor(mydata$long / cellsize) * cellsize + cellsize/2
latrounded <- floor(mydata$lat / cellsize) * cellsize + cellsize/2
cellcentre <- paste(longrounded,latrounded, sep=":")
allcells <- unique(cellcentre)
mynetw <- matrix(0, length(allcells), length(allspecies))It is also crucial to name the rows and columns of the network so that we can interpret the results of the Modularity Analysis.
for (i in 1:length(mydata[,1]))
mynetw[ match(cellcentre[i],allcells) , match(mydata$species[i], allspecies) ] <- 1
rownames(mynetw) = allcellsNow we come to the actual Modularity Analysis. We need to have the R library rnetcarto installed and load it.
colnames(mynetw) = allspecies
library(rnetcarto)The command to start the analysis is simply:
mymodules <- netcarto(mynetw, bipartite=TRUE)This may take a bit of time, but after talking to colleagues who have got experience with other software it seems it is actually reasonably fast - for a Modularity Analysis.
Once the analysis is done, we may first wonder how many modules, which we will subsequently interpret as bioregions, the analysis has produced.
length(unique(mymodules[]$module))For publication we obviously want a decent map, but that is beyond the scope of this post. What follows is merely a very quick and dirty way of plotting the results to see what they look like, but of course the resulting coordinates and module numbers can also be used for fancier plotting. We split the latitudes and longitudes back out of the cell names, define a vector of colours to use for mapping (here thirteen; if you have more modules you will of course need a longer vector), and then we simply plot the cells like some kind of scatter plot.
allcells2 <- strsplit( as.character( mymodules[]$name ), ":" )
allcells_x <- as.numeric(unlist(allcells2)[c(1:(length(allcells)))*2-1])There we are. Modularity analysis with the R library rnetcarto is quite easy, the main problem was building the network.
allcells_y <- as.numeric(unlist(allcells2)[c(1:(length(allcells)))*2])
mycolors <- c("green", "red", "yellow", "blue", "orange", "cadetblue", "darkgoldenrod", "black", "darkolivegreen", "firebrick4", "darkorchid4", "darkslategray", "mistyrose")
plot(allcells_x, allcells_y, col = mycolors[ as.numeric(mymodules[]$module) ], pch=15, cex=2)
As an example I have done an analysis with all Australian (and some New Guinean) lycopods, the dataset I mentioned in the previous post. It plots as follows.
There are, of course, a few issues here. The analysis produced six modules, but three of them, the green, orange and light blue ones, consist of only two, one and one cells, respectively, and they seem biologically unrealistic. They may be artifacts of not having cleaned the data as well as I would for an actual study, or represent some kind of edge effect. The remaining three modules are clearly more meaningful. Although they contain some outlier cells, we can start to interpret them as potentially representing tropical (red), temperate (yellow), and subalpine/alpine (dark blue) assemblies of species, respectively.
Despite the less than perfect results I hope the example shows how easy it is to do such a Modularity Analysis, and if due diligence is done to the spatial data, as we would do in an actual study, I would also expect the results to become cleaner.