Homework activities

Activity 1

General instructions

Complete this activity as a qmd file and commit the source code, along with the rendered pdf, to your course activities repository by 20 September

  • Save the .qmd source code for this file to your local directory.
  • Edit the document with your responses to the questions, and render the document into an pdf file.
  • Commit and push all materials to your “Activities” git repository.

Note: if all else fails, you can email this activity to me as a Word or PDF file.

Grading: Each paper (or dataset, for undergraduate students) you describe is worth 4 points. An additional 5 points are for writing your responses in a qmd file, rendering it to a PDF, and making it available in your course repository.

Instructions for graduate students

Identify five primary research papers that you feel are important for you to know inside-and-out as part of your dissertation research, and which you feel might be good candidates for our course semester project. For each paper, complete the following information:

  • Full article citation:

  • In your own words, describe (a) what was the motivation for this study; (b) what data the authors generated to address their question; (c) how the data were analyzed (both in terms of what approaches, and what code/statistical packages); and (d) how the results from this paper shape your work.

  • Have the authors made the data and/or code available as supplemental materials or through public repositories? If so, please include a link to the data.

  • What steps would you have to take to reproduce 1–2 key results from this paper?

Instructions for undergraduate students

Explore the data options available for your semester project through the LTER Data Sampler R package: https://lter.github.io/lterdatasampler/. Read each dataset’s vignette, which is available through the “Articles” option in the top menubar. For five of the most interesting datasets, adderess the following questions:

  • Name and source of dataset:

  • Why was this dataset originally collected? (I.e. what questions were the authors exploring when collecting these data?)

  • What is the type of data available? Describe both the technical features of the files (e.g. “a CSV table with 10 columns and 950 rows”), and the contents themselves (e.g. “columns 2 and 3 describe the latitude and longitude; column 4 describes the species name, […]”)

  • Brainstorm 1–2 figures that you would like to create with these data.1


Activity 2

NoneGeneral instructions

Complete this activity as a qmd file and commit the source code, along with the rendered pdf, to your course activities repository by 15 October

  • Save the .qmd source code for this file to your local directory.
  • Edit the document with your responses to the questions, and render the document. I’d prefer if you can make a pdf file.
  • Commit and push all materials to your “Activities” git repository.

Part 1: Background and data exploration

Background

The R package buoydata, developed and maintained by Andy Beet (link), provides fantastic utility functions for retrieving data from NOAA’s National Buoy Data Center (link). I have used this package to download data from two Buoys (buoy IDs 42084 and BURL1), both located at SouthWest Pass, Louisiana (link).

Both buoys were located in the same region; BURL1 has been collecting data since the 1980s, and 42084 was deployed in 2019. Both are still in operation today, but buoy BURL1 has had lots of missing data values over the last few years. So, in this exercise, we will combine data from both to explore trends in climatic data.

To begin this exercise, download the data from the two buoys to your computer using the code below.

library(tidyverse)

b_42084 <- readRDS(url("https://gitlab.com/gklab/teaching/rr-f26/-/raw/main/weekly-activities/week-07/b_42084-data.rds?ref_type=heads&inline=false"))
b_burl1 <- readRDS(url("https://gitlab.com/gklab/teaching/rr-f26/-/raw/main/weekly-activities/week-07/burl1-data.rds?ref_type=heads&inline=false"))

#### NOTE
# If you want to download the data "straight from the source" rather
# than from the git repository where I have uploaded it, 
# you can use the code below. 
# install.packages('pak')
# pak::pak("NOAA-EDAB/buoydata")
# library(tidyverse)
# library(buoydata)
# b_burl1 <- get_buoy_data("BURL1")
# b_42084 <- get_buoy_data("42084")

Data exploration

Before starting any visualization, it is good practice to understand the structure of the data. Let’s use the buoydata to understand the structure of the dataset. Run the code below to install and load the buoydata package, and use the function get_variables() for a peek into the content of the data.

# Install packages `pak` and `buoydata` (uncomment these lines if needed)
# install.packages('pak')
# pak::pak("NOAA-EDAB/buoydata")

library(tidyverse)
library(buoydata)
buoydata::get_variables()

Question 1: What are some variables that we can work with from the data? If I wanted to conduct an analysis of wind speed, which column would I use for my analysis?

ADD YOUR RESPONSE HERE

Part 2: Data streamlining

The two files we’ve downloaded comprise lots of data! Each tibble has 27 columns, and combine to over 425,000 rows. Let’s simplify our life a bit.

The goal of our exercise is to make the plot show in ?@fig-airtemp, which means we really only need a two columns worth of information – the time of measurement, and the air temperature. So, let’s use select to streamline both datasets:

b_42084 <-
  b_42084 |> 
  select(time, ATMP)
  
b_burl1 <-
  b_burl1 |> 
  select(time, ATMP)

On your own, use the function dim to check that each tibble now comprises just 2 columns, but that the number of rows is unchanged.

Next, instead of working with two different tibbles, we can combine the two tibbles together – this is especially easy because both tibbles contain the same columns. We can use the function bind_rows to, well, bind the rows together into just one tibble:

combined_data <-
  bind_rows(b_burl1, b_42084)

In class, we mentioned the package lubridate, one of the sub-packages within tidyverse. Lubridate is especially useful for timeseries data, like what we are working with here.

Let’s use functions from this package to add columns to this combined tibble to know just the year, month, and date of the observations:

combined_data <-
  combined_data |> 
  mutate(
    # Make new columns called year_, month_, and doy_
    # using the function year(), month(), and yday() from lubridate
    year_ = year(time),
    month_ = month(time),
    # DOY stands for day-of-year:
    # (e.g. Jan 1 is DOY 1, Feb 15 is DOY 45, ...)
    doy_ = yday(time)
  )

The tibble now has five columns:

head(combined_data)
# A tibble: 6 × 5
  time                 ATMP year_ month_  doy_
  <dttm>              <dbl> <dbl>  <dbl> <dbl>
1 1984-02-21 12:00:00  11.1  1984      2    52
2 1984-02-21 13:00:00  10.4  1984      2    52
3 1984-02-21 14:00:00  11.1  1984      2    52
4 1984-02-21 15:00:00 NaN    1984      2    52
5 1984-02-21 16:00:00  10.8  1984      2    52
6 1984-02-21 17:00:00  10.6  1984      2    52

We are now ready to work towards visualizing trends in air temperature over time at this location.

Part 3: Visualizing the data

Ultimately, our goal is to make the following visualization:

Let’s work towards this in chunks.

Step 1: Add a new column indicating the decade

The plot we wish to make has data summarized by decade, but this information is currently not directly available in combined_data. This means we will have to make a new column that states the decade based on the information in the year column:

combined_data |> 
  # We will use a clever trick to name the decade: 
  # For any given year, we can use the first three numbers of the year
  # as the decade name, e.g. if the year is 1989, it belongs to decade "198",
  # and the year 1995 belongs to the decade "199"
  mutate(decade = str_extract(year_, "..."))
# A tibble: 425,398 × 6
   time                 ATMP year_ month_  doy_ decade
   <dttm>              <dbl> <dbl>  <dbl> <dbl> <chr> 
 1 1984-02-21 12:00:00  11.1  1984      2    52 198   
 2 1984-02-21 13:00:00  10.4  1984      2    52 198   
 3 1984-02-21 14:00:00  11.1  1984      2    52 198   
 4 1984-02-21 15:00:00 NaN    1984      2    52 198   
 5 1984-02-21 16:00:00  10.8  1984      2    52 198   
 6 1984-02-21 17:00:00  10.6  1984      2    52 198   
 7 1984-02-21 18:00:00  10.7  1984      2    52 198   
 8 1984-02-21 19:00:00  10.6  1984      2    52 198   
 9 1984-02-21 20:00:00  10.7  1984      2    52 198   
10 1984-02-21 21:00:00  10.8  1984      2    52 198   
# ℹ 425,388 more rows

This is a good start, but ultimately we don’t just want 198, 199, 200, 201, and 202 as our decades, we want the labels to say “1980s”, “1990s”, and so forth. Let’s use the function paste0 to make the labels:

combined_data <-
  combined_data |> 
  # We will use a clever trick to name the decade: 
  # For any given year, we can use the first three numbers of the year
  # as the decade name, e.g. if the year is 1989, it belongs to decade "198",
  # and the year 1995 belongs to the decade "199"
  mutate(decade = str_extract(year_, "...")) |> 
  mutate(decade = paste0(decade, "0s"))

head(combined_data)
# A tibble: 6 × 6
  time                 ATMP year_ month_  doy_ decade
  <dttm>              <dbl> <dbl>  <dbl> <dbl> <chr> 
1 1984-02-21 12:00:00  11.1  1984      2    52 1980s 
2 1984-02-21 13:00:00  10.4  1984      2    52 1980s 
3 1984-02-21 14:00:00  11.1  1984      2    52 1980s 
4 1984-02-21 15:00:00 NaN    1984      2    52 1980s 
5 1984-02-21 16:00:00  10.8  1984      2    52 1980s 
6 1984-02-21 17:00:00  10.6  1984      2    52 1980s 

Step 2: Getting daily averages

The dataset we’re working with has >425,000 rows! Each day has several 24 rows, as the buoy records air temperature once every hour. Ultimately, we just want to know, for each day of year in each decade, what the average air temperature was on that day – that’s what ?@fig-airtemp shows.

Given that we want to get the average air temperature per DOY per decade, we should be thinking that we want to do some grouped operations.

Question 2. Complete the code below to use create new columns that indicate, for each DOY in each decade, the average temperature

combined_data <- 
  combined_data |> 
  # NOTE: Fill in the blanks in the next two lines:
  group_by(____, ____) |> 
  mutate(mean_airtemp = mean(____, na.rm = T))

To know whether your answer is correct, run the following code and make sure that you get the same data as shown below.

combined_data |> 
  slice(1)
# A tibble: 1,830 × 7
# Groups:   doy_, decade [1,830]
   time                 ATMP year_ month_  doy_ decade mean_airtemp
   <dttm>              <dbl> <dbl>  <dbl> <dbl> <chr>         <dbl>
 1 1985-01-01 00:00:00 NaN    1985      1     1 1980s          15.0
 2 1990-01-01 00:00:00  12.7  1990      1     1 1990s          13.9
 3 2000-01-01 00:00:00  19.9  2000      1     1 2000s          14.2
 4 2010-01-01 00:00:00  13.3  2010      1     1 2010s          15.8
 5 2020-01-01 00:00:00 NaN    2020      1     1 2020s          17.3
 6 1985-01-02 00:00:00  17.4  1985      1     2 1980s          12.7
 7 1990-01-02 00:00:00   9.8  1990      1     2 1990s          14.1
 8 2000-01-02 00:00:00  18.8  2000      1     2 2000s          14.0
 9 2010-01-02 00:00:00   9.5  2010      1     2 2010s          13.7
10 2020-01-02 00:00:00 NaN    2020      1     2 2020s          17.7
# ℹ 1,820 more rows

If you completed Question 2 correctly, you should now have all the data necessary to complete a rough draft of the plot.

Question 3. Complete the code below to create the plot shown under this question.

combined_data |> 
  # Fill in the blanks below
  # NOTE: there are three blanks because you should have 
  # three arguments within aes().
  ggplot(aes(_____, ______, _____)) + 
  geom_line()

Nice! After question 3, we seem to be very close to our final graph – in fact, all the remaining steps seem to be aesthetic modifications. Let’s turn to those next.

Question 4. For this step, complete as much of the code below as you can to recreate the graph shown in ?@fig-airtemp.

# Make a vector that has the start date for each month (and also 
# the end date for December)
month_startdates <- c(0,cumsum(days_in_month(1:11)), 365)
names(month_startdates) <- NULL

combined_data |> 
  ggplot(aes(
    # NOTE: fill in these blanks in the same way as you did in Question 3:
    ____, ______, ______
  )) + 
  geom_line() +
  
  # This sets the color scale to use the viridis palette
  # instead of the default. 
  scale_color_viridis_d() +
  
  # This next chunk adds vertical lines at the start of each month
  geom_vline(xintercept = month_startdates,
             linetype = "dashed", color = "grey25", linewidth = 0.25) +
  
  # This makes it so that the X-axis has ticks at each of the month start DOYs instead
  # of every 10 days or whatever the default spread is
  scale_x_continuous(breaks = month_startdates, 
                     expand = c(0)) +
  
  # This adds the month names to the top of the plot
  annotate("text", x = month_startdates[1:12], y = Inf, 
           # Let's use "month.abb", a default variable within `R` with the short names for each month
           label = month.abb, 
           size = 3, 
           # NOTE: Look up the help page for the `annotate()` function
           # to learn how to italicize the label text
           color = 'grey25', vjust = 1, hjust = -1) +
  labs(# NOTE: use this to set the X- and Y- axis labels, as well
       # as the plot title and subtitle
    ) +
  theme_classic() +
  theme(plot.subtitle = element_text(face = "italic")) # This italicizes the subtitle

Part 4: Extending beyond

Now that you have considerable expertise in visualization, challenge yourself to make the following plot. Note that to make this plot, I used only the data available within combined_data. Each point represents the mean air temperature of all days within a given month-decade combination. The error bars show the standard deviation of the air temperatures.

Hint: I used geom_pointrange() to create a plot with points and errorbars. I used the sd() function to calculate standard deviations.

Footnotes

  1. If you are stuck, consider exploring the R graph gallery to explore different ways to visualize data↩︎