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
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.
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.
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.