Part 1

Load the libraries

library(readr)
library(ggplot2)
library(dplyr)
library(dasehr)

Open the Nitrate exposure via WA public waterways data from the dasehr package.

(You can also access it at the link www.daseh.org/data/Nitrate_Exposure_for_WA_Public_Water_Systems_byquarter_data.csv)

Then, use the provided code to compute a data frame nitrate with aggregate summary of exposure level: average exposed population (pop_exposed_to_exceedances) for each year (year).

nitrate_agg <- nitrate %>%
  group_by(year) %>%
  summarise(exposed_pop_avg = mean(pop_exposed_to_exceedances))

nitrate_agg
## # A tibble: 22 × 2
##     year exposed_pop_avg
##    <dbl>           <dbl>
##  1  1999            15  
##  2  2000             0  
##  3  2001            76.8
##  4  2002          3976. 
##  5  2003          3636. 
##  6  2004          3138. 
##  7  2005          2968. 
##  8  2006          2618. 
##  9  2007          1916. 
## 10  2008          1728. 
## # ℹ 12 more rows

1.1

Use ggplot2 package make plot of average exposed population (exposed_pop_avg; y-axis) for each year (year; x-axis). You can use lines layer (+ geom_line()) or points layer (+ geom_point()), or both!

Assign the plot to variable my_plot. Type my_plot in the console to have it displayed.

# General format
ggplot(???, aes(x = ???, y = ???)) +
  ??? +
  ???
my_plot <-
  ggplot(nitrate_agg, aes(x = year, y = exposed_pop_avg)) +
  geom_line() +
  geom_point()

my_plot

1.2

“Update” your plot by adding a title and changing the x and y axis titles.

my_plot <- my_plot +
  labs(
    x = "Year",
    y = "Average population exposed",
    title = "Average population exposed to excess nitrate in public water sources, 1999-2020"
  )

my_plot

1.3

Use the scale_x_continuous() function to plot the x axis with the following breaks c(1999, 2001, 2003, 2005, 2007, 2009, 2011, 2013, 2015, 2017, 2019).

# General format
my_plot<- my_plot +
  scale_x_continuous(?????)
my_plot <- my_plot +
  scale_x_continuous(
    breaks = c(1999, 2001, 2003, 2005, 2007, 2009, 2011, 2013, 2015, 2017, 2019)
  )

my_plot

1.4

Observe several different versions of the plot by displaying my_plot while adding a different “theme” to it.

# General format
my_plot + theme_bw()
my_plot + theme_bw()

my_plot + theme_classic()

my_plot + theme_dark()

my_plot + theme_gray()

my_plot + theme_void()

Practice on Your Own!

P.1

Create a boxplot (with the geom_boxplot() function) using the nitrate data, where quarter is plotted on the x axis and pop_on_sampled_PWS is plotted on the y axis.

nitrate %>%
  ggplot(aes(x = quarter, y = pop_on_sampled_PWS)) +
  geom_boxplot()

Part 2

2.1

Use the provided code to compute a data frame nitrate_agg_2 with aggregate summary of WA Nitrate data: population exposed to less than 10 ug/L of nitrate in the water (sum of pop_0-3ug/L, pop_>3-5ug/L, and pop_>5-10ug/L) – separately for each year (year) and for each quarter (quarter.

nitrate_agg_2 <- nitrate %>%
  group_by(year, quarter) %>%
  summarise(pop_less_than_10ug_perL = sum(`pop_0-3ug/L`, `pop_>3-5ug/L`, `pop_>5-10ug/L`))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
nitrate_agg_2
## # A tibble: 88 × 3
## # Groups:   year [22]
##     year quarter pop_less_than_10ug_perL
##    <dbl> <chr>                     <dbl>
##  1  1999 Q1                        67807
##  2  1999 Q2                        55688
##  3  1999 Q3                       550650
##  4  1999 Q4                        26389
##  5  2000 Q1                         5996
##  6  2000 Q2                       157428
##  7  2000 Q3                        20752
##  8  2000 Q4                       360235
##  9  2001 Q1                        49702
## 10  2001 Q2                        46259
## # ℹ 78 more rows

2.2

Use ggplot2 package to make a plot showing trajectories of total population exposed to less than 10 ug/L of nitrate (pop_less_than_10ug_perL; y-axis) over year (year; x-axis), where each quarter type has a different color (hint: use color = type in mapping).

# General format
ggplot(???, aes(
  x = ???,
  y = ???,
  color = ???
)) +
  geom_line() +
  geom_point()
ggplot(nitrate_agg_2, aes(
  x = year,
  y = pop_less_than_10ug_perL,
  color = quarter
)) +
  geom_line() +
  geom_point()

2.3

Redo the above plot by adding a faceting (+ facet_wrap( ~ quarter, ncol = 2)) to have data for quarter in a separate plot panel.

Assign the new plot as an object called facet_plot.

facet_plot <- ggplot(nitrate_agg_2, aes(
  x = year,
  y = pop_less_than_10ug_perL,
  color = quarter
)) +
  geom_line() +
  geom_point() +
  facet_wrap(~quarter, ncol = 2)

facet_plot

2.4

Observe what happens when you remove either geom_line() OR geom_point() from one of your plots above.

# These elements are removed from the plot, like layers

Practice on Your Own!

P.2

Modify facet_plot to remove the legend (hint use theme() and the legend.position argument) and change the names of the axis titles to be “Population exposed to less than 10 ug/L of nitrate in water” for the y axis and “Year” for the x axis.

facet_plot <- facet_plot +
  theme(legend.position = "none") +
  labs(
    y = "Population exposed to less than 10 ug/L of nitrate in water",
    x = "Year"
  )

facet_plot

P.3

Modify facet_plot one more time with a fun theme! Look into the ThemePark package It has lots of fun themes! Try one out! Remember you will need to install it using remotes::install_github("MatthewBJane/ThemePark")and load in the library.

# remotes::install_github("MatthewBJane/ThemePark")
library(ThemePark)

facet_plot + theme_grand_budapest()