```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Part 1

Load the libraries

```{r, message=FALSE}
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`). 

```{r}

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

nitrate_agg
```

### 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 = ???)) +
  ??? +
  ???
```

```{r 1.1response}

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. 

```{r 1.2response}
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(?????)
```

```{r 1.3response}
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()
```

```{r 1.4response}
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. 

```{r P1response}
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`. 

```{r}

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`))

nitrate_agg_2
```

### 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()
```

```{r 2.2response}
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`.

```{r 2.3response}

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.

```{r 2.4response}
# 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. 

```{r P.2response}
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](https://github.com/MatthewBJane/ThemePark) 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. 

```{r P.3response}
# remotes::install_github("MatthewBJane/ThemePark")
library(ThemePark)

facet_plot + theme_grand_budapest()
```
