Advanced R in Biology Second Assignment - Tidyverse

Author

Riley Taylor

print("The tidyverse packages I will be using in my code in addition to ggplot2 are dplyr and readr. The dataset from the lterdatasampler package I wil be using is ntl_airtemp, containg the Daily average air temperature data for Madison Wisconsin from 1869 to 2019")
[1] "The tidyverse packages I will be using in my code in addition to ggplot2 are dplyr and readr. The dataset from the lterdatasampler package I wil be using is ntl_airtemp, containg the Daily average air temperature data for Madison Wisconsin from 1869 to 2019"
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
library(lterdatasampler)
write_csv(ntl_airtemp,"ntl_airtemp.csv")
ntl_airtemp <- read_csv("ntl_airtemp.csv")
Rows: 55151 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl  (2): year, ave_air_temp_adjusted
date (1): sampledate

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ntl_airtemp
# A tibble: 55,151 × 3
   sampledate  year ave_air_temp_adjusted
   <date>     <dbl>                 <dbl>
 1 1870-06-05  1870                  20  
 2 1870-06-06  1870                  18.3
 3 1870-06-07  1870                  17.5
 4 1870-06-09  1870                  13.3
 5 1870-06-10  1870                  13.9
 6 1870-06-11  1870                  15  
 7 1870-06-12  1870                  14.4
 8 1870-06-13  1870                  13.9
 9 1870-06-14  1870                  17.5
10 1870-06-15  1870                  18.3
# ℹ 55,141 more rows

I am installing the full package lterdatasampler, as well as loading tidyverse and readr (though loading readr is a bit redundant, it does not always work for me in positron unless loaded separately). I am converting the information from lterdatasampler I am interested into a csv, and then I can use readr from tidyverse to read the information, have it stored in a separate window and its own variable.

ntl_airtemp_plot1 <- ntl_airtemp |> group_by(year) |> filter(!is.na(ave_air_temp_adjusted)) |> summarize(mean_temp = mean(ave_air_temp_adjusted)) |> ggplot(aes(x = year, y = mean_temp,)) + geom_point() + labs( y = "Average Air Temperature (C)", x = "Year")
ntl_airtemp_plot1

Here is the first plot I am creating in ggplot2. I am taking the temperature table and grouping by year, taking the average temperature of each year (while removing NaN values) and turning that average temperature for each year (in celsius) into a scatter plot. I am combining many dplyr verbs (such as group_by, filter, summarize, and mean) with the pipe in order to create a final plot using ggplot2.

library(lubridate)
dates <- ntl_airtemp |>  mutate(date = ntl_airtemp$sampledate, months = month(date))
winter <- dates |> filter(between(months, 1, 3)) |>  mutate(season = "winter")
spring <- dates |> filter(between(months, 4, 6)) |>  mutate(season = "spring")
summer <- dates |> filter(between(months, 7, 10)) |>  mutate(season = "summer")
fall <- dates |> filter(between(months, 11, 12)) |>  mutate(season = "fall")
average_seasonal_weather <- bind_rows(winter, spring, summer, fall)
ntl_airtemp_plot2 <- ggplot(data = average_seasonal_weather, aes(x = months, y =ave_air_temp_adjusted)) + geom_point() + facet_wrap(~season)
ntl_airtemp_plot2

This is the more involved graph out of the 2 needed for the assignment. I loaded up the lubridate package as I was going to be working with date formats in my table. I first created a new dataframe called dates where I am taking all of the sampledate variables from the ntl_airtemp table and separating out just the months from the date configuration into a new column called months within dates. From there, I am grouping the month column into the 4 seasons (not based on anything truly factual, just what made the most sense to me from 1-12). I created the season column as I new I wanted to facet_wrap each season in my final graph in order to view them separately. I am using a new dplyr verb mutate in order to create a new column that manipulates information from the existing ones in my ggplot.