
---
title: "5 In-class Exercise: Color Theory"
output:
  html_document
---

```{r, include=FALSE}
# Do not edit this code chunk
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE)
```

# Apply Color Theory to Scatterplots and Barplots

# Pre-requisites:

Since we will be working with color palettes today, we are going to install the `RColorBrewer` package in `R`. Enter the following in your `R` Console.

`install.packages("RColorBrewer")`

> Note: Remember, we only need to install packages once to have their functions available for us in our `R` environments.

# Step 1: Load Packages

Load the package and the dataset we need to work on our analysis today.

```{r}
library(tidyverse)
library(RColorBrewer)
library(nycflights13)
```

# Step 2: View the `flights` Data Frame Using Two Approaches

```{r}
# Write your code below

```

```{r}
# Write your code below

```

# Scatterplot

# Step 3: Create a Scatterplot with Two Continuous Variables

```{r}
ggplot(data = flights,
       aes(x = dep_delay,
           y = arr_delay)) +
  geom_point()
```

# Step 4: Map the Variable `origin` to the Color Aesthetic

```{r}
# Add your code
ggplot(data = flights,
       aes(x = dep_delay,
           y = arr_delay)) +
  geom_point()
```

# Step 5: Recreate the Figure but Change Default "color" of Points by Adding a Palette Layer

For palette choices, you can run the code below.

```{r}
RColorBrewer::display.brewer.all()
```

There are three types of color palettes that can be added to a plot:

* Sequential: A uni-directional ordering of shades.
* Diverging: A bi-directional ordering of shades.
* Qualitative: A discrete set of colors. 

```{r}
# Paste the code below and adjust it

```

# Barplot

# Step 6: Study Codes of Figure 2.26 in ModernDive

The code chunk below will repoduce Figure 2.26, but here is the [link](https://moderndive.com/2-viz.html) too. 

```{r}
ggplot(data = flights, 
       aes(x = carrier, 
           fill = origin)) +
  geom_bar(position = position_dodge(preserve = "single"))
```

# Step 7: Recreate Figure 2.26 but Change Default "fill" Color of Bars by Adding a Palette Layer

Add a palette layer of your choice. To determine how to do this, direct your attention to the *Color and Fill Scales* section of the [ggplot2 cheatsheet](https://github.com/rstudio/cheatsheets/blob/main/data-visualization-2.1.pdf).

```{r}
# Add your code
ggplot(data = flights, 
       aes(x = carrier, 
           fill = origin)) +
  geom_bar(position = position_dodge(preserve = "single"))

```

# Step 8: Recreate Figure 2.26 but Change Default "fill" Color of Bars by Manually Changing Colors in a Layer

Run `colors()` in your console to get English names of all colors in R.

Manually change colors of your choice. To determine how to do this, direct your attention to the *Scales* section of the [ggplot2 cheatsheet](https://github.com/rstudio/cheatsheets/blob/main/data-visualization-2.1.pdf).

```{r}
# Add your code
ggplot(data = flights, 
       aes(x = carrier, 
           fill = origin)) +
  geom_bar(position = position_dodge(preserve = "single"))

```

# Step 9: Recreate Figure 2.26 but Change Default "fill" Color of Bars by Manually Changing Colors in a Layer Using [Hex Codes](https://www.color-hex.com/)

```{r}
# Add your code
ggplot(data = flights, 
       aes(x = carrier, 
           fill = origin)) +
  geom_bar(position = position_dodge(preserve = "single"))

```
