Suggested Reading:

Chapters 1.1, 1.2, 2, 3, 4, 9 and 10 in: Wickham, H. 2009. ggplot2: Elegant Graphics for Data Analysis. Springer. DOI: Stanford Full Text

The sections on mutating and filtering joins in this vignette

RStudio has produced several helpful cheatsheets on data management and visualization with ggplot2 and tidyr. You may want to view or print one or several of them.

Before Class

Download this R script and save it in your code folder in the research project you created. Be sure to remove the .txt extension that may have been added when you saved the file to your computer so that the file ends in .R. You may want to run the first part of the script that downloads the data to be sure that there will be no issues with data access during class.

tidyverse

Key Points

  • Data should be organized so that observations are in rows and attributes (variables) are in columns.
  • The dplyr package contains functions for manipulating data:
    • pivot_longer and pivot_wider change the format of data tables.
    • separate and unite change the format of variables.
    • filter creates a subset of a data table.
    • mutate creates new variables.
    • group_by and summarize are used to summarize observations according to the values of their variables.
    • joins (e.g., left_join) add columns from different tables, matching observations based on the keys.
  • The pipe function %>% is used to link several operations together (e.g. function composition).
  • The ggplot2 package is set of functions for visualizing data.
    • A ggplot2 plot contains three components: (1) the data, (2) the aesthetic mappings between variables and visual properties, and (3) layers describing how to display the observations.
    • The aesthetic mapping aes must minimally describe which variables define the plot coordinate space.
    • facet_wrap creates plots for specified data subsets.

Manipulating data with dplyr

Open the R project that you created on the first day of class. Then when R Studio opens, open the R script file that you downloaded for today’s class (see above). This script downloads a data set from our course website and then proceeds to do a bit of manipulation/summarization of the data:

  1. PineRidge_30x30Tree_ALL is a table where observations are trees, with information on their size (dbh.cm), species, decay class (if a snag), and cavity information.
  2. PineRidge_CWD_ALL is a table where each observation is a portion of a transect covered by coarse woody debris (i.e., “cwd”). Notice the plot codes match the plot codes in the trees dataset.

Working in pairs, execute each line of code under the “tidyverse” section in the Rscript and then add a comment (using #) above each line of code with a description of what the code does.

When you have finished commenting the code, try to complete the following challenge:

Challenge 1:

  1. Summarize the CWD table such that we get the total coarse woody debris volume (line cover x width x height) per plot.code and each row corresponds with exactly one plot.code. Name the total volume column “CWD_volume”.
  2. Join the Tree and CWD tables, and name the result “allData”. Check that you have not lost rows (i.e., plot.codes) or metadata.

Plotting data with ggplot2

Working with your partner, use the summarize tables from the challenge above to create the following plots using ggplot2:

Challenge 2:

  1. A boxplot of mean DBH by fire severity.
  2. Modify the above plot so that two plots are shown, one for the Dawes (burn = “D”) and Fort Robinson (burn = “FR”).

Loops

In coding, a “loop” is a workflow that repeats the same instructions or processes the same information, often with slight variations, until receiving the order to stop. Loops are SUPER useful for data wrangling and analyses. For instance, what if you had 100 different linear regression models using the same data? A sad way to tackle this problem would require a bunch of copying/pasting code. Thankfully, computers are very good at doing drudgery very quickly with loops! There are multiple kinds of loops (e.g., while, for, repeat), but we’re going to focus on for loops here.

Working in pairs, execute each line of code under the “Loops” section in the Rscript and then add a comment (using #) above each line of code with a description of what the code does.

Challenge 3:

  1. Using the joined Tree/CWD table, create a loop to run the models provided in the Rscript and outputs a list where each element in the list is a model object.
  2. This isn’t a loop, but it’s useful addendum: give each model in the list a descriptive name.

Functions

R is full of functions that will do most of what you’ll need for data wrangling and modeling. But sometimes, you will find yourself in very specific situation for which there is no ready-made function. Or you could find yourself wanting to generalize a workflow such that it can work in more than a single situation. In these cases, it pays to be able to write your own functions!

For example, consider the loop challenge above. That loop was super useful for those specific ten models, but you’re going to be running many models in this course. Wouldn’t it be useful to have a function that can take any number of models, run them, and name them?

Also, there’s a best practice for writing functions: annotate, annotate, annotate. Annotate to make every part of the function as clear as possible and provide a clear description of the function. For example, pick your favorite function and look up the documentation. The documentation provides a description, the syntax, the arguments, any relevant details, and output values.

Working in pairs, execute each line of code under the “Functions” section in the Rscript and then add a comment (using #) above each line of code with a description of what the code does.

Challenge 4:

  1. Using the loop and naming code you wrote in the previous challenge, create a function that can run any number of linear regression models through a loop and then name them.