User Tools

Site Tools


Bird Abundance and Diversity in Sweden's Agricultural Landscapes

Ryan Clark

Swedish University of Agricultural Sciences


General framework of the analysis

  1. Background: Farmland bird populations across Europe have been declining rapidly due to increase of agricultural land use and intensity. Some types of agricultural land use, or farm management, may benefit some bird species. Data on bird species presence and abundance has been collected at many farms across Sweden between 2006 - 2011. Some farms have been surveyed twice (i.e. in two different years between 2006 - 2011).
  2. keywords: agriculture, farmland birds, R, species richness, Sweden

Project objectives

To use R (along with BASH) to process and analyse the data and examine trends in farmland bird diversity and/or abundance in Sweden across years (i.e. between 2006 - 2011), and/or between different regions in Sweden, or different farm-crop types. For the result I will produce figure(s) summarising the trends/patterns, and potentially map(s) as well. If possible, I will conduct a basic statistical test.


The core data consists of the following: an Excel (xlsx) file containing 24452 observations (i.e. rows) with data collected for up to 47 variables (i.e. columns), and an associated polygon shapefile of the Sweden farms where the data in the Excel file were collected.

Vector data

Three shapefiles:

  • polygon shapefile of the farm fields in Sweden where the data in the “FarmBirds” Excel file were collected between 2006 - 2011.
  • polygon shapefile of the counties of Sweden
  • polygon shapefile of the land boundary of Sweden

Other data sources

An Excel (xlsx) file, “FarmBirds”, containing 24452 observations (i.e. rows) with data collected for up to 47 variables (i.e. columns). The variables include information about each farm and field (e.g. farm name and ID, field ID, crop or land use type, and centroid coordinates), the year and exact date of each bird survey, the number observed of each of 29 bird species, and some additional variables not used in the analysis.


The script is primarily based in R and consists of two main parts. The first part is used to import the xlsx file into R and create a summarised version of the data. The reading of the xlsx file in R may first require the use of BASH to complete this process on a remote server. Once this is completed, R is used to create a summarised version of the data, and to add two new variables containing the total abundance and the richness (i.e. number of different species) observed on each farm for each year surveyed. The second part of the script, also in R, was written to create two boxplots, one of the total abundance per year, and one of the richness.

Computational implementation

- mycode
### 1. The first step is to read the xls file into R:
### Depending on the size of the xls file, and the limited RAM,
### it may be better to convert the xls file to a csv on a remote server as follows:
 ## copy xls file to remote server:
 scp /media/sf_ProjectShare/FarmBirds.xlsx  user@remoteserver:~ost4sem/MyProject/FarmBirds.xlsx
 ## if needed - copy the R directory from local to remote server:
 scp -r R user@remoteserver:~$R
 ## Log in to the remote server, enter R and install and load the "gdata" package:
 ssh user@remoteserver
 ## convert xls to csv file:
 xls2csv(xls = "ost4sem/MyProject/FarmBirds.xlsx")
 ## quit R and move the resulting csv file to the project directory on the remote server:
 mv tmp/RtmpKI5hk8/fileb83e1230546e.csv home/user/ost4sem/MyProject
 ## exit the remote server, and copy csv file from the remote server to the local working directory:
 scp user@remoteserver:~/ost4sem/MyProject/fileb83e1230546e.csv RyWork/MyProject/
 ## rename csv file:
 mv /home/user/RyWork/MyProject/fileb83e1230546e.csv /home/user/RyWork/MyProject/FarmBirds_Rcsv.csv
 ## Enter R, or open R console (RStudio), and read in the "FarmBirds_Rcsv.csv" as a data.frame:
 FarmBirds <- read.csv(file="/home/user/RyWork/MyProject/FarmBirds_Rcsv.csv",sep=",")
 ## for later steps - copy shapefiles to be processed in R to the local working directory:
 cp /media/sf_ProjectShare/Sweden* /home/user/RyWork/MyProject/
 cp /media/sf_ProjectShare/Inventoried_Fields* /home/user/RyWork/MyProject/
### 1. The first step is to read the xls file into R: 
 ## Enter R or open the R console (e.g. RStudio), install and load the "gdata" package,
 ## which contains various tools for data manipulation, particularly for reading and converting xls files:
## Read the xls file into R (this creates a temporary csv that is treated as a data frame):
 FB <- read.xls("/home/user/RyWork/MyProject/FarmBirds.xlsx")
### 2. To obtain summed counts of each bird species for each year on each farm:
 ## Step 1: Define the columns for grouping, and those that contain data to be evaluated in the data frame (FB)
 ## representing the original xls (or csv) file from part 1 (above):
 colnames(FB) # to determine the order numbers of columns in the data frame for use in the following steps
 GroupCols <- colnames(FB[,3:4]) # creates a vector of the column names to be used for grouping in the ddply command (plyr pacakge)
 BirdNames <- colnames(FB[,19:47]) # creates a vector of the column names containing counts of individual bird species
 ## Step 2: Use ddply command (plyr package) to create a summarised data frame of the original data
 FB.SUMp1 <- ddply(FB, GroupCols, numcolwise(sum))
 ## Step 3: Using the mutate command (plyr package) - add columns to FB.SUMp1 containing -
 ## the total number of birds counted, and the number of different species observed at least once 
  # if needed - use colnames to determine the order numbers of columns in the FB.SUMp1 data frame for use in -
  # subsequent processes (this can also be determined from info in the Workspace window, or using View(FB.SUMp1))
  # create a new data frame, maintaining FB.SUMp1, and add two new columns with total sums from two different rowSums functions.
  FB.SUMp2 <- mutate(FB.SUMp1, TotalIndiv = rowSums(FB.SUMp1[,3:31]), Richness = rowSums(FB.SUMp1[,3:31]>0))
### Create some basic boxplots to view general trends
 ## Boxplot of bird abundance
 boxplot(TotalIndiv~YEAR, data=FB.SUM4, varwidth=T, main = "Median Birds Counted per Farm per Year",
        ylab = "Abundance (count of individuals)", xlab = "Year")
 ## Boxplot of bird species richness
 boxplot(Richness~YEAR, data=FB.SUM4, varwidth=T, main = "Median Bird Species Richness per Farm per Year",
        ylab = "Number of Species", xlab = "Year")


  • Graphic 1.

My graphic caption

  • Graphic 2. My graphic caption
  • Map 1.

wikistudholland/utwente_rayan.txt · Last modified: 2021/01/20 20:36 (external edit)