Installation

RStudio is an environment for developing using R. It can be downloaded here. You will need the Desktop version for your computer.

RStudio basics

RStudio has four panels:

Top left: the editor. This panel will be closed when you start RStudio. Here you can edit and execute scripts. The editor has a button to run the current line or selection, and a button to run the whole script.

Bottom left: the console. Here you can enter commands or debug your code.

To get help about a function, type the function name with a question mark in front:

?data.frame

If no documentation is found, you can try:

??data.frame

R packages

R packages are reusable libraries of code. To install and load packages from the console (e.g. the ggplot2 R package), do:

install.packages("ggplot2")library(ggplot2)

This only works for packages which are published on CRAN. Nowadays packages are often published on GitHub. To install those packages, we can use the install_github function in the devtools package. Here we use the double colon syntax to automatically load the devtools package.

Excel files

Excel files can be read and written using the xlsx and openxlsx packages. Depending on your system configuration, you may experience problems installing either of these packages (for example, xlsx has a dependency on Java). The openxlsx packages requires a recent R version.

install.packages("openxlsx")library(openxlsx)

read.xlsx() takes two parameters: the name of the Excel file, and the sheet you want to read. The sheet can either be a name or an index, in this case 1 in order to read the first sheet.

Shapefiles

Shapefiles can be read using the rgdal package. The example below also transforms the data, so it can easily be visualized using ggplot2:

library(maptools)library(rgdal)library(ggplot2)download.file("http://iobis.org/geoserver/OBIS/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=OBIS:summaries&outputFormat=SHAPE-ZIP",destfile="summaries.zip")unzip("summaries.zip")shape<-readOGR("summaries.shp",layer="summaries")shape@data$id<-rownames(shape@data)df<-fortify(shape,region="id")data<-merge(df,shape@data,by="id")# plot the number of species
ggplot()+geom_polygon(data=data,aes(x=long,y=lat,group=group,fill=s),color='gray',size=.2)+scale_fill_distiller(palette="Spectral")

Aggregation

Restructuring (matrix to long format)

Biodiversity data is often provided as a site x species matrix. The reshape2 package can be used to convert these matrices to a long table format. To demonstrate this functionality, let’s load a site x species matrix which is included in the vegan package (which focuses on biodiversity data analysis).

The dataset which we will use is the BCI dataset, these are tree counts in plots on Barro Colorado Island. Load the data with data():

data("BCI")

Each row in this matrix represents a plot. This matrix doesn’t have a column for site/plot names so let’s add that:

BCI$plot<-row.names(BCI)

Now use the melt function to convert from matrix to long format. Pass the following arguments: variable.name (this corresponds to the columns, so scientific names), value.name (a name for the values), and id.vars (the not measured variables, in this case plot).