Learning Objectives
Following this assignment students should be able to:
- Read files in R
- Understand data manipulation functions in R
- Summarise dataframes
- Write csv files
- Create plots with lattice and ggplot
Exercises
Using a two-year manipulative experiment, Root et al. 2017 were interested in studying how a gradient in intensive forest management (from light to intensive) affects both moth species richness and abundances. They have placed the data file on the Dryad repository:
Root HT, Verschuyl J, Stokely T, Hammond P, Scherr MA, Betts MG (2016) Data from: Plant diversity enhances moth diversity in an intensive forest management experiment. Dryad Digital Repository. https://doi.org/10.5061/dryad.47200
-
Accept the assignment here and clone the repository in a local directory using Rstudio
-
Download the data here and copy the files
MothCommunities2012.csv
andMothCommunities2013.csv
into thedata
folder. -
Open the Rproj file in Rstudio
-
Open the file
0_cleaning_data.R
and import both Moth communities files using the functionsread_csv()
orread.csv()
. Name the objectscomu_2012
andcomu_2013
-
Use
str()
to show the structure of the data frame and its individual columns. - The following code calculates the total moth richness and abundance for the gradient in intensive forest management in each study block.
comu_2012_dt<- comu_2012 %>% group_by(block, treatment) %>% summarise(Total_sp=sum(richness,na.rm=TRUE), Total_abundance=sum(totabund,na.rm=TRUE)) %>% mutate(year=2012)
- Add comments to each line of the code explaining what it does
- Using the same code, create a new object called
comu_2013_dt
with the 2013 treatment data - Do not forget to commit your changes on your git console (select files -> commit -> write commit)
-
Comment in the code how the object
Comunitity_total
was obtained, and check the dimensions of the object or data frame using the functiondim()
-
Write a csv file into the
outputs
folder calledmothComunity_2012_2013.csv
using the functionwrite.csv()
-
Open the
1_Exploring_data.R
file, read the data as it is written on the file. -
Using the code in the task 6, calculate the total abundance of per moths species per treatment using the object
Sp_values
. Called the new object or data frameSp_Abun_table
- The following code filters the total abundance of the Agrotis vancouverensis species in each forest management treatment
Sp_Abun_table %>% filter(Species=="Agrotis.vancouverensis")
- Using the same code, filter the total abundance for the following species: “Drasteria divergens, Hydriomena manzanita, Xanthorhoe defensaria”
- Contrast the abundances of the above species with those shown in the original paper here
-
Change the class of the
year
variable in theTotal_comun
data frame as factor, using the functionas.factor()
- The following code plots the number of species in the four forest management treatments
ggplot(Total_comun, aes(y = Total_sp, x = treatment, fill=year)) +
geom_boxplot() +
geom_jitter(aes(shape = year,colour=year), width = 0.1)
- Using this code, create a plot for the total abundance in the four forest management
- Print the number of species and the abundance plots in a pdf using the function
pdf()
and save the files into thefigs
folder -
Contrast the total abundance figure with the figure 2D of the original paper (see paper here)
- Do not forget to commit your changes on your git console and push your changes to github (select files -> commit -> write commit -> push)
Expected assignment outputs
As as reference, the following are the outputs expected for some of the tasks.
Task | Expected output |
---|---|
6 | output, .txt |
7 | output, .txt |
10 | output, .txt |
11 | output, .txt |
12 | output, .txt |
13 | output, .png |
Want a challenge?
-
Check the R file
0_reshaping_community_data.R
into the Rscripts folder and comment the code explaining what it does -
Try to reproduce the plot in the task 13 using other plot functions such as
boxplot()
orbwplot()
from the lattice package. You might need to use other functions such asstripchart()
to display the points on the boxplots.