Visualising networks and calculating network parameters in R

Downloading and Updating R

R is a free to download statistical and graphical software language which is hugely versatile. R’s capabilities are augmented by packages which are free to download. We will use the bipartite package which allows us to visualise interactions of two trophic levels and to calculate indices to describe their topology.

If you have not done so previously download and install the statistical package “R” from http://cran.uk.r-project.org/ and RStudio, a popular user interface, from https://rstudio.com/products/rstudio/download/#download

Most issues with this practical are due to base R not being up to date and packages not being compatible. You may have downloaded R to use in modules during Junior Sophister and may not have updated it since. To check whether R is up to date run the following code:

sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 17763)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_Ireland.1252  LC_CTYPE=English_Ireland.1252   
## [3] LC_MONETARY=English_Ireland.1252 LC_NUMERIC=C                    
## [5] LC_TIME=English_Ireland.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] compiler_4.0.2  magrittr_1.5    tools_4.0.2     htmltools_0.5.0
##  [5] yaml_2.2.1      stringi_1.4.6   rmarkdown_2.4   knitr_1.30     
##  [9] stringr_1.4.0   xfun_0.15       digest_0.6.25   rlang_0.4.6    
## [13] evaluate_0.14

The first information printed by this function tells you what version of R you are using. To check the most up to date version visit https://cran.rstudio.com/ and under the heading “Source Code for all Platforms” the first bullet point will give the most up to date version. If the version printed out by the sessionInfo() function and that on the CRAN website match, great, you are up to date.

If not, you need to update R. Run the following code, but first remove the #s, then close down R studio and open it back up. You now should have the most up to date version of R and the rest of the following code should run fine.

#install.packages("installr")
library(installr)

#updateR()

Installing Bipartite Package

If you don’t have the bipartite package install it by removing the # and run the ìnstall.packages("bipartite) code. Then load the bipartite library

#install.packages("bipartite")
library(bipartite)

Plotting Flores Network

Plotting the Flores Network and saving it

#load flores data
data(olesen2002flores)
#view flores data
olesen2002flores

#plot flores bipartite network
flores <- plotweb(olesen2002flores)

flores

#To save the network the ggsave function in the ggplot2 package is useful. If you need to download ggplot2 remove the # from the line below.
#install.packages("ggplot2")
library(ggplot2)

#save flores bipartite network as a tiff, can be scaled without losing res and can use in office suit (word and powerpoint)

ggsave("Flores_Bipartite_Plot.tiff", flores, device = "tiff", dpi =300, width = 30, height = 20, units = "cm")

View the Flores data as a matrix, with interaction strength indicated by depth of shading

visweb(olesen2002flores)

Calculating Network Parameters

Generate the parameters for Flores data which describe the properties of the network

#calculate the network parameters
networklevel(olesen2002flores)

#save parameters as a dataframe
flores_net_params <- as.data.frame(networklevel(olesen2002flores))
#create a column of parameter names
flores_net_params$Params <- rownames(flores_net_params)
#rename column names
colnames(flores_net_params) <- c("Value", "Parameter")
#reorder the columns using packages tidy and dplyr. If you need to download  remove the # from the next two lines
#install.packages("tidyr")
#install.packages("dplyr")

library(tidyr)
library(dplyr)
flores_net_params <- as_tibble(flores_net_params) %>% 
  select(Parameter, Value)

#save data to csv file
write.csv(flores_net_params, "Flores_Net_Params.csv")

Redo For Aigrettes Data

To import the second data set, repeat steps above, but use “olesen2002aigrettes” in place of “olesen2002flores”.