1 Overview

Claremont, CA has had a local winter Christmas Bird Count (CBC for short) station since 1972. We are going to interact with the CBC data for four focal species:

In the following exercises, please discuss as a group:

Note that in the assignment ClaremontCBC, you’ll see a ClaremontCBC.R script that you can use to follow along with this discussion activity.

2 Loading packages

First, let’s load the packages that we’ll need.

###==================================================
### Loading packages & keys
###==================================================
library("dplyr")    # load package for wrangling data
library("readr")    # package for reading tabular data
library("ggplot2")  # package for plotting

2.1 Importing the data

###==================================================
### Importing the data
###==================================================
claremontCBC <- read_tsv("https://raw.githubusercontent.com/chchang47/BIOL104PO/master/data/ClaremontCBCdata.txt") # pulling in the spreadsheet from this internet location and storing it in claremontCBC

### Inspecting the data
claremontCBC %>%
  head() %>%
  View()

# The above is equivalent to:
# View( head( claremontCBC )) # delete the leading # to run

The Claremont CBC data has these four columns:

  • Species: the shortened code for each species
  • Year: the year of the CBC observation
  • Number: the number of each species observed in that year
  • NumNormal: Number/effort, where effort is the number of people participating that year

3 Filtering the data

Your task is to:

First, let’s consider how to filter the data to just one of the species.

### Create your subset data
clareSub <- claremontCBC %>%
  filter(Species=="...") # select one of the 4 species and replace the ... with its short code name

### View your subset data
clareSub

3.1 Generating a plot

Of the different plots (see Week2b FMI), because we have observations of distinct numeric x- and y-variables (x = time and y = species counts), a scatterplot would be appropriate.

### Creating a scatterplot
  # Note that we will store the scatterplot in an object named "p" for plot
p <- ggplot(clareSub, aes(x=Year, y=Number)) # specifying an aesthetic where x is Year and y is the count of that species
p <- p + geom_point() # adding on points to create a scatterplot
p <- p + labs(x="...", y="...") # changing the default x and y axis labels to more informative labels (replace the ... with your text)
p # calling p which will be displayed in the plot viewer

### Slightly more advanced move to save plots using commands
  # ?ggsave # uncomment and run to load the help page
#ggsave(plot= p, filename="BirdPlot.jpeg") # replace filename with something more informative; remove leading # to run

3.1.1 Exercise

How would you repeat the plot above but have NumNormal on the y-axis? Why might this be more appropriate in some cases?

3.2 Plotting two or more species

We’ll start by modifying the data filter to include two species.

First, let’s consider how to filter the data to just one of the species.

### Create your subset data
clareSub <- claremontCBC %>%
  filter(Species=="..." | Species=="...") # select two of the 4 species and replace the ... with their short code name
  # NB: the | symbol means OR - match species 1 OR species 2

### View your subset data
clareSub

3.3 Generating a plot

Of the different plots (see Week2b FMI), because we have observations of distinct numeric x- and y-variables (x = time and y = species counts), a scatterplot would be appropriate.

### Creating a scatterplot
  # Note that we will store the scatterplot in an object named "p" for plot
p <- ggplot(clareSub, aes(x=Year, y=Number, color=Species)) # specifying an aesthetic where x is Year and y is the count of that species
  # We're adding on another dimension to the visualization, which is using color to differentiate species
p <- p + geom_point() # adding on points to create a scatterplot
p <- p + labs(x="...", y="...") # changing the default x and y axis labels to more informative labels (replace the ... with your text)
p <- p + theme_classic() # choosing a simpler black and white background
p # calling p which will be displayed in the plot viewer

### Slightly more advanced move to save plots using commands
  # ?ggsave # uncomment and run to load the help page
#ggsave(plot= p, filename="BirdPlot2.jpeg") # replace filename with something more informative; remove leading # to run

3.3.1 Exercise

How would you repeat the plot above but have NumNormal on the y-axis? Why might this be more appropriate in some cases?

4 Answers to exercises

4.1 Single-species version

### Creating a scatterplot
  # Note that we will store the scatterplot in an object named "p" for plot
p <- ggplot(clareSub, aes(x=Year, y=NumNormal)) # specifying an aesthetic where x is Year and y is the count of that species
p <- p + geom_point() # adding on points to create a scatterplot
p <- p + labs(x="Year", y="Count (normalized)") # changing the default x and y axis labels to more informative labels (replace the ... with your text)
p # calling p which will be displayed in the plot viewer

### Slightly more advanced move to save plots using commands
  # ?ggsave # uncomment and run to load the help page
#ggsave(plot= p, filename="BirdPlot.jpeg") # replace filename with something more informative; remove leading # to run

4.2 Multi-species version

### Creating a scatterplot
  # Note that we will store the scatterplot in an object named "p" for plot
p <- ggplot(clareSub, aes(x=Year, y=NumNormal, color=Species)) # specifying an aesthetic where x is Year and y is the count of that species
  # We're adding on another dimension to the visualization, which is using color to differentiate species
p <- p + geom_point() # adding on points to create a scatterplot
p <- p + labs(x="Year", y="Count (normalized)") # changing the default x and y axis labels to more informative labels (replace the ... with your text)
p <- p + theme_classic() # choosing a simpler black and white background
p # calling p which will be displayed in the plot viewer

### Slightly more advanced move to save plots using commands
  # ?ggsave # uncomment and run to load the help page
#ggsave(plot= p, filename="BirdPlot2.jpeg") # replace filename with something more informative; remove leading # to run