3 Sampling an ecological network

Eva Knop and Vincent Grognuz
session 19/03/2024

Field work

In this exercise session you will collect data outdoor and build your own pollination network. Instructions are provided in the slides of the presentation, but we will also show you practically what you are expected to do in the Irchel part.

During the field work you will have to fill the file workspace/03-19_sampling_an_ecological_network/species_interactions_protocol.csv with your data.

Data analysis and report preparation

After having collected your data, you will need to process it to visualize the network that you have created. In this section we will show you how to:

  1. Load the data you collected in the field
  2. Convert the data into an igraph object or an incidence matrix
  3. Visualize your network
  4. Compile a Rmd file to produce your report

A template to produce your report is also provided in the folder /workspace/03-19_sampling_an_ecological_network.

The deadline to submit the report for this exercise session is Saturday, April 13th at 1:00 pm.

If you have questions, please do not hesitate to contact us.

3.1 Practical demonstration

To walk you through all the steps needed to prepare your report we will work with a mock data set.

Load data

Assuming this is the data you have collected, let us load it into an R script (remember to adapt the path to the right file in your report!).

# Read data into R. 
data = read.csv("~/ecological_networks_2024/downloads/Data/sampling_an_ecological_network/species_interactions_protocol_example.csv",sep = ",")

Visualize data

This is how your data looks like

library(knitr)
library(formattable)
kable(data)       # well-displayed in RMarkdown 
plant insect date time temperature
Primula.vulgaris Bombus.terrestris 22.03.22 14:02 18
Primula.vulgaris Bombus.terrestris 22.03.22 14:02 18
Primula.vulgaris Episyrphus.balteatus 22.03.22 14:02 18
Primula.vulgaris Hymenoptera.sp2 22.03.22 14:15 18
Salix.sp Bombus.m1 23.03.22 14:05 18
Salix.sp Bombus.m1 23.03.22 14:05 18
Yellow.flower .m1 Bombus.m1 24.03.22 14:20 18
Gallanthus.nivalis Apis.mellifera 25.03.22 11:12 15
Taraxacum.officinalis Episyrphus.balteatus 26.03.22 10:30 15
Salix.sp Episyrphus.balteatus 23.03.22 10:25 15
Primula.vulgaris Hymenoptera.sp1 22.03.22 10:22 15
Primula.vulgaris Hymenoptera.sp2 22.03.22 10:12 14
Primula.vulgaris Hymenoptera.sp2 22.03.22 10:05 14
Salix.sp Bombus.m1 22.03.22 9:47 13
Salix.sp Bombus.m1 22.03.22 10:00 13
#formattable(data) # R script 

Now that you have loaded your dataset, you need to convert it into an igraph object, i.e., “something” that is suitable to be visualized and to be analyzed quantitatively as a network. As seen in the first exercise session, the edge list is a possible format to represent a network. In the file helper.R we have provided a function that transforms your raw data into an edge list.

# select the first 2 columns with species names   
my_data = data[,1:2] 

# Convert the raw data into an edge list by means of the function convert_data_to_edge_list(my_data)
source("~/ecological_networks_2024/downloads/Functions/helper.R")
my_edge_list <- convert_data_to_edge_list(my_data)

# show the edge-list dataframe 
kable(my_edge_list)        # well-displayed in RMarkdown 
species1 species2 connection_strength
Primula.vulgaris Bombus.terrestris 2
Primula.vulgaris Episyrphus.balteatus 1
Primula.vulgaris Hymenoptera.sp2 3
Primula.vulgaris Hymenoptera.sp1 1
Salix.sp Episyrphus.balteatus 1
Salix.sp Bombus.m1 4
Yellow.flower .m1 Bombus.m1 1
Gallanthus.nivalis Apis.mellifera 1
Taraxacum.officinalis Episyrphus.balteatus 1
#formattable(my_edge_list) # R script 

Following the procedure we have applied in the session toolkit for network analysis, you can convert your edge list into an igraph object

my_graph <- graph_from_data_frame(my_edge_list, directed = FALSE)

# make the graph bipartite 
V(my_graph)$type <- bipartite_mapping(my_graph)$type 

# set the layout for your plot
LO = layout_as_bipartite(my_graph)

# visualize the network
plot(my_graph, vertex.label=V(my_graph)$Name,
     vertex.size=8, vertex.label.dist=9, layout=LO[,2:1],  
     vertex.label.degree = pi*V(my_graph)$type,vertex.label.cex=0.8,vertex.color = rgb(0.8,0.4,0.3,0.8),
     vertex.label.family="Times",vertex.label.color="black",edge.color="black",main = "My Network")

If you wish to visualize your network using the package bipartite, you first need to convert the igraph object into an incidence matrix (see first exercise session); then you can plot it

# convert the igraph object into incidence matrix 
inc_matrix <- as_incidence_matrix(
  my_graph,
  attr = "connection_strength",
  names = TRUE,
  sparse = FALSE
)

# convert elements into numeric values 
class(inc_matrix) <-"numeric"

# remove NA values (previous empty strings)
inc_matrix[which(is.na(inc_matrix) == T)]<-0

# plot the network
#plotweb(inc_matrix) # simple version 
plotweb(inc_matrix, 
        method="normal", text.rot=90, 
        bor.col.interaction="gray40",plot.axes=F, col.high="blue", labsize=1, 
        col.low="darkgreen", y.lim=c(-1,3), low.lablength=40, high.lablength=40)

3.2 Taxize API

In research projects it is often important to have taxonomic information about species that compose an ecological network. We would like to mention the taxize package which was developed right for this purpose. To install taxize you should run from the Console this command

# uncomment the following line (remove `#`) if needed 
#install.packages("taxize")

but it should already be installed on the RStudio Server. Below you can see an example of how to query the taxonomy for the names Primula.vulgaris and Bombus.m1

library(taxize)
name_to_resolve1 <-  "Primula.vulgaris"  
name_to_resolve2 <-  "Bombus.m1" 

primula <- gnr_resolve(
  name_to_resolve1,
  canonical = TRUE,
  data_source_ids = "1,4,8,9,11,167,179",
  best_match_only = TRUE,
  fields = "all"
)

bombus <- gnr_resolve(
  name_to_resolve2,
  canonical = TRUE,
  data_source_ids = "1,4,8,9,11,167,179",
  best_match_only = TRUE,
  fields = "all"
)

# bind the 2 rows (query results) together 
res <- rbind(primula, bombus)

# show results 
#formattable(res)

Since the res dataframe has many columns, it is better to run this chunk of code in a standalone R script. We will do that using the file 03-21_run_taxize.R in the workspace folder.

The commands in the previous chunk provide the best response that taxize finds in some reference databases. Sometimes it can also be useful to get all the results found by taxize and this is obtained running the following command:

res_all <- gnr_resolve(name_to_resolve1,
                       canonical = TRUE,
                       fields = "all")

After having resolved the taxonomy of some selected names, you can summarize the result in a csv file and display it in your report

# Read data into R. 
taxon_df = read.csv("~/ecological_networks_2024/downloads/Data/sampling_an_ecological_network/species_taxonomy.csv",sep = ",")

# show results 
kable(taxon_df)
name taxonomic_res kingdom order family species
Primula.vulgaris Subspecies Plants Ericales Primulaceae Primula acaulis
Bombus.m1 Genus Animals Hymenoptera Apidae NA

3.3 Your task

We expect you to repeat the analysis shown above with the dataset you collected during the block course. We ask you to upload the raw data you collected as a .csv file onto the server filling the template provided by us.
Please store your dataset in the folder workspace/03-19_sampling_an_ecological_network/.
Later you can use the 03-19_sampling_an_ecological_network.Rmd script therein to produce your report in html format. Once you are done, please download the whole folder and upload it on OLAT.

If you encounter technical troubles, please ask Alessandro to help you.

Bonus

Use taxise to resolve the taxonomy of the following names
* Apis mellifera
* Bombus terrestris
* Episyrphus balteatus
* Salix.sp

Add the outcome of your query into the file species_taxonomy.csv given as example in the assignment folder of today’s session (columns names represent submitted name, taxonomic resolution, kingdom, order, family, species). Besides the names listed above, you can attempt to resolve also some plants or insects that appear in your network.