exclude: true

```{r setup, message=FALSE, warning=FALSE, include=FALSE}
options(
  htmltools.dir.version = FALSE, # for blogdown
  width=80
)

knitr::opts_chunk$set(
  cache = TRUE, fig.align = "center", fig.retina = 2, dpi = 150
)

library(tidyverse)
library(palmerpenguins)
```

---
class: middle, center

```{r echo=FALSE, out.width="45%"}
knitr::include_graphics("imgs/hex-ggplot2.png")
```

---

## The Grammar of Graphics

- Visualisation concept created by Leland Wilkinson (1999)
- to define the basic elements of a statistical graphic 

- Adapted for R by Hadley Wickham (2009)
- consistent and compact syntax to describe statistical graphics
- highly modular as it breaks up graphs into semantic components 

- ggplot2 is not meant as a guide to which graph to use and how to best convey your data (more on that later), but it does have some strong opinions.

---

## Terminology

A statistical graphic is a...

- mapping of **data**
- which may be **statistically transformed** (summarized, log-transformed, etc.)
- to **aesthetic attributes** (color, size, xy-position, etc.)
- using **geometric objects** (points, lines, bars, etc.)
- and mapped onto a specific **facet** and **coordinate system**

---

## Anatomy of a ggplot call

```r
ggplot(
data = [dataframe], 
mapping = aes(
x = [var x], y = [var y], 
color = [var color], 
shape = [var shape],
...
)
) +
geom_[some geom](
mapping = aes(
color = [var geom color],
...
)
) +
... # other geometries
scale_[some axis]_[some scale]() +
facet_[some facet]([formula]) +
... # other options
```

---

## Data - Palmer Penguins

Measurements for penguin species, island in Palmer Archipelago, size (flipper length, body mass, bill dimensions), and sex.

.pull-left-narrow[
```{r echo=FALSE, out.width="100%"}
knitr::include_graphics("imgs/penguins.png")
```
]
.pull-right-wide[
```{r echo=FALSE}
options(width = 70)
```

```{r}
library(palmerpenguins)
penguins
```

```{r echo=FALSE}
options(width = 65)
```
]

---

## A basic ggplot


.pull-left[
```{r penguins, fig.show = "hide"}
ggplot(
  data = penguins, 
  mapping = aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) +
  geom_point() +
  labs(
    title = "Bill depth and length",
    subtitle = paste(
      "Dimensions for Adelie, Chinstrap,",
      "and Gentoo Penguins"
    ),
    x = "Bill depth (mm)", 
    y = "Bill length (mm)",
    color = "Species"
  )
```
]

.pull-right[
```{r ref.label = "penguins", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

class: middle

# Text <-> Plot

---

.midi[
> **Start with the `penguins` data frame**
]

.pull-left[
```{r penguins-0, fig.show = "hide", warning = FALSE}
ggplot(data = penguins) #<<
```
]
.pull-right[
```{r ref.label = "penguins-0", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> **map bill depth to the x-axis**
]

.pull-left[
```{r penguins-1, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(x = bill_depth_mm) #<<
) 
```
]
.pull-right[
```{r ref.label = "penguins-1", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> **and map bill length to the y-axis.**
]

.pull-left[
```{r penguins-2, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm #<<
  )
)
```
]
.pull-right[
```{r ref.label = "penguins-2", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> and map bill length to the y-axis. 
> **Represent each observation with a point**
]

.pull-left[
```{r penguins-3, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) + 
  geom_point() #<<
```
]
.pull-right[
```{r ref.label = "penguins-3", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> and map bill length to the y-axis. 
> Represent each observation with a point
> **and map species to the color of each point.**
]

.pull-left[
```{r penguins-4, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) + 
  geom_point(
    mapping = aes(color = species) #<<
  )
```
]
.pull-right[
```{r ref.label = "penguins-4", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> and map bill length to the y-axis. 
> Represent each observation with a point
> and map species to the color of each point.
> **Title the plot "Bill depth and length"**
]

.pull-left[
```{r penguins-5, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) +
  geom_point(
    mapping = aes(color = species)
  ) +
  labs(title = "Bill depth and length") #<<
```
]
.pull-right[
```{r ref.label = "penguins-5", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> and map bill length to the y-axis. 
> Represent each observation with a point
> and map species to the color of each point.
> Title the plot "Bill depth and length", 
> **add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins"**
]

.pull-left[
```{r penguins-6, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) +
  geom_point(
    mapping = aes(color = species)
  ) +
  labs(
    title = "Bill depth and length",
    subtitle = paste("Dimensions for Adelie,", #<<
                     "Chinstrap, and Gentoo", #<<
                     "Penguins") #<<
  ) 
```
]
.pull-right[
```{r ref.label = "penguins-6", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> and map bill length to the y-axis. 
> Represent each observation with a point
> and map species to the color of each point.
> Title the plot "Bill depth and length", 
> add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", 
> **label the x and y axes as "Bill depth (mm)" and "Bill length (mm)", respectively**
]

.pull-left[
```{r penguins-7, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) +
  geom_point(
    mapping = aes(color = species)
  ) +
  labs(
    title = "Bill depth and length",
    subtitle = paste("Dimensions for Adelie,",
                     "Chinstrap, and Gentoo",
                     "Penguins"),
    x = "Bill depth (mm)", #<<
    y = "Bill length (mm)" #<<
  )
```
]
.pull-right[
```{r ref.label = "penguins-7", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> and map bill length to the y-axis. 
> Represent each observation with a point
> and map species to the color of each point.
> Title the plot "Bill depth and length", 
> add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", 
> label the x and y axes as "Bill depth (mm)" and "Bill length (mm)", respectively,
> **label the legend "Species"**
]

.pull-left[
```{r penguins-8, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) +
  geom_point(
    mapping = aes(color = species)
  ) +
  labs(
    title = "Bill depth and length",
    subtitle = paste("Dimensions for Adelie,",
                     "Chinstrap, and Gentoo",
                     "Penguins"),
    x = "Bill depth (mm)",
    y = "Bill length (mm)",
    color = "Species" #<<
  ) 
```
]
.pull-right[
```{r ref.label = "penguins-8", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> and map bill length to the y-axis. 
> Represent each observation with a point
> and map species to the color of each point.
> Title the plot "Bill depth and length", 
> add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", 
> label the x and y axes as "Bill depth (mm)" and "Bill length (mm)", respectively,
> label the legend "Species", 
> **and add a caption for the data source.**
]

.pull-left[
```{r penguins-9, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) +
  geom_point(
    mapping = aes(color = species)
  ) +
  labs(
    title = "Bill depth and length",
    subtitle = paste("Dimensions for Adelie,",
                     "Chinstrap, and Gentoo",
                     "Penguins"),
    x = "Bill depth (mm)",
    y = "Bill length (mm)",
    color = "Species",
    caption = "Source: palmerpenguins package" #<<
  )
```
]
.pull-right[
```{r ref.label = "penguins-9", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

.midi[
> Start with the `penguins` data frame,
> map bill depth to the x-axis
> and map bill length to the y-axis. 
> Represent each observation with a point
> and map species to the color of each point.
> Title the plot "Bill depth and length", 
> add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", 
> label the x and y axes as "Bill depth (mm)" and "Bill length (mm)", respectively,
> label the legend "Species", 
> and add a caption for the data source.
> **Finally, use the viridis color palete for all points.**
]

.pull-left[
```{r penguins-10, fig.show = "hide", warning = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) +
  geom_point(
    mapping = aes(color = species)
  ) +
  labs(
    title = "Bill depth and length",
    subtitle = paste("Dimensions for Adelie,",
                     "Chinstrap, and Gentoo",
                     "Penguins"),
    x = "Bill depth (mm)",
    y = "Bill length (mm)",
    color = "Species",
    caption = "Source: palmerpenguins package"
  ) +
  scale_color_viridis_d() #<<
```
]
.pull-right[
```{r ref.label = "penguins-10", echo = FALSE, warning = FALSE, out.width = "95%", fig.width = 8}
```
]

---

## Argument names

Often we omit the names of first two arguments when building plots with `ggplot()`.

.pull-left[
```{r named-args, eval = FALSE}
ggplot(
  data = penguins,
  mapping = aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) +
  geom_point(
    mapping = aes(color = species)
  ) +
  scale_color_viridis_d()
```
]
.pull-right[
```{r not-named-args, eval = FALSE}
ggplot(
  penguins,
  aes(
    x = bill_depth_mm,
    y = bill_length_mm
  )
) +
  geom_point(
    aes(color = species)
  ) +
  scale_color_viridis_d()
```
]

.footnote[**Note** that `ggplot` and `geom_*` swap the order of the `data` and `mapping` arguments.]

---

class: middle

# Aesthetics

---

## Aesthetics options

Commonly used characteristics of plotting characters that can be **mapped to a specific variable** in the data are

- `color`
- `shape`
- `size`
- `alpha` (transparency)

Different geometries have different aesthetics that can be used - see the [ggplot2 geoms](https://ggplot2.tidyverse.org/reference/index.html#Geoms) help files for listings.


* Aesthetics given in `ggplot` apply to all `geom`s.

* Aesthetics for a specific `geom` can be overridden with the `geom_*`'s aesthetics.

---

## color

.pull-left[
```{r color, fig.show = "hide", warning = FALSE}
ggplot(
  penguins,
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) + 
  geom_point(
    aes(color = species) #<<
  )
```
]
.pull-right[
```{r ref.label = "color", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Shape

Mapped to a different variable than `color`

.pull-left[
```{r shape-island, fig.show = "hide", warning = FALSE}
ggplot(
  penguins,
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) +
  geom_point(
    aes(
      color = species, #<<
      shape = island   #<<
    )
  )
```
]
.pull-right[
```{r ref.label = "shape-island", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Shape

Mapped to same variable as `color`

.pull-left[
```{r shape-species, fig.show = "hide", warning = FALSE}
ggplot(
  penguins,
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) +
  geom_point(
    aes(
      color = species, #<<
      shape = species  #<<
    )
  )
```
]
.pull-right[
```{r ref.label = "shape-species", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Size

Using a fixed value (note this value is outside of the `aes` call)

.pull-left[
```{r size1, fig.show = "hide", warning = FALSE}
ggplot(
  penguins,
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) +
  geom_point(
    aes(
      color = species,
      shape = species
    ),
    size = 3 #<<
  )
```
]
.pull-right[
```{r ref.label = "size1", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Size

Mapped to a variable

.pull-left[
```{r size2, fig.show = "hide", warning = FALSE}
ggplot(
  penguins,
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) +
  geom_point(
    aes(
      color = species,
      shape = species,
      size = body_mass_g
    ),
  )
```
]
.pull-right[
```{r ref.label = "size2", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Alpha

.pull-left[
```{r alpha, fig.show = "hide", warning = FALSE}
ggplot(
  penguins,
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) +
  geom_point(
    aes(
      color = species,
      shape = species,
      alpha = body_mass_g
    ),
    size = 3
  )
```
]
.pull-right[
```{r ref.label = "alpha", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Mapping vs. setting

- **Mapping:** Determine an aesthetic (the size, alpha, etc.) of a geom based on the values of a variable in the data
- goes into `aes()` as an argument which is then an argument of `ggplot2` or `geom_*()`.

- **Setting:** Determine an aesthetic (the size, alpha, etc.) of a geom **not** based on the values of a variable in the data
- goes directly into `geom_*()` as an argument.

---

class: middle

# Faceting

---

## Faceting

- Smaller plots that display different subsets of the data

- Useful for exploring conditional relationships and large data

- Sometimes refered to as "small multiples"

---

## facet_grid

.pull-left[
```{r facet, warning = FALSE, fig.show = "hide"}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) + 
  geom_point() +
  facet_grid( species ~ island ) #<<  
```
]

.pull-right[
```{r ref.label = "facet", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Compare with ...

.pull-left[
```{r facet_comp, warning = FALSE, fig.show = "hide"}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) + 
  geom_point(
    aes(
      color = species,
      shape = island
    ), 
    size = 3
  )
```
]

.pull-right[
```{r ref.label = "facet_comp", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## facet_grid (cols)

.pull-left[
```{r facet2, warning = FALSE, fig.show = "hide"}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) + 
  geom_point() +
  facet_grid(  ~ species ) #<<  
```
]

.pull-right[
```{r ref.label = "facet2", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## facet_grid (rows)

.pull-left[
```{r facet3, warning = FALSE, fig.show = "hide"}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) + 
  geom_point() +
  facet_grid( species ~ . ) #<<  
```
]

.pull-right[
```{r ref.label = "facet3", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]



---

## facet_wrap

.pull-left[
```{r facetwrap, warning = FALSE, fig.show = "hide"}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) + 
  geom_point() +
  facet_wrap(~ species) #<<
```
]

.pull-right[
```{r ref.label = "facetwrap", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## facet_wrap

.pull-left[
```{r facetwrap2, warning = FALSE, fig.show = "hide"}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm
  )
) + 
  geom_point() +
  facet_wrap(~ species, ncol = 2) #<<
```
]

.pull-right[
```{r ref.label = "facetwrap2", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Faceting and color

.pull-left[
```{r facet-color-legend, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm, 
    color = species
  )
) +
  geom_point() +
  facet_grid(species ~ sex)
```
]
.pull-right[
```{r ref.label = "facet-color-legend", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Hiding redundancy

.pull-left[
```{r facet-color-no-legend, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm, 
    color = species
  )
) +
  geom_point() +
  facet_grid(species ~ sex) +
  guides(color = FALSE) #<<
```
]

.pull-right[
```{r ref.label = "facet-color-no-legend", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---
class: middle

# A brief plot Tour of ggplot2

---

## Histograms

.pull-left[
```{r hist1, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g, 
  )
) +
  geom_histogram()
```
]

.pull-right[
```{r ref.label = "hist1", echo = FALSE, warning = FALSE, out.width = "95%", message = FALSE}
```
]

---

## Histograms - bins

.pull-left[
```{r hist3, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g, 
  )
) +
  geom_histogram(bins = 50)
```
]

.pull-right[
```{r ref.label = "hist3", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Histograms - binwidth

.pull-left[
```{r hist2, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g, 
  )
) +
  geom_histogram(binwidth = 250)
```
]

.pull-right[
```{r ref.label = "hist2", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]



---

## Histograms - color

.pull-left[
```{r hist5, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g, 
    color = species
  )
) +
  geom_histogram(bins = 20)
```
]

.pull-right[
```{r ref.label = "hist5", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Histograms - fill

.pull-left[
```{r hist4, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g, 
    fill = species
  )
) +
  geom_histogram(bins = 20)
```
]

.pull-right[
```{r ref.label = "hist4", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Histograms - position

.pull-left[
```{r hist_pos, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g, 
    fill = species
  )
) +
  geom_histogram(
    bins = 20,
    position = "identity",
    alpha = 0.5
  )
```
]

.pull-right[
```{r ref.label = "hist_pos", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]


---

## Histograms - facets

.pull-left[
```{r hist6, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g, 
    fill = species
  )
) +
  geom_histogram(bins = 20) +
  facet_grid(species ~ .) + 
  guides(fill = FALSE)
```
]

.pull-right[
```{r ref.label = "hist6", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Density plot

.pull-left[
```{r dens1, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g
  )
) +
  geom_density()
```
]

.pull-right[
```{r ref.label = "dens1", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Density plot - fill

.pull-left[
```{r dens3, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g, 
    fill = species
  )
) +
  geom_density(alpha = 0.25)
```
]

.pull-right[
```{r ref.label = "dens3", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Density plot - adjust

.pull-left[
```{r dens2, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g,
    fill = species
  )
) +
  geom_density(
    adjust = 0.5, 
    alpha = 0.25
  )
```
]

.pull-right[
```{r ref.label = "dens2", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Violin plot

.pull-left[
```{r violin, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = species,
    y = body_mass_g,
    fill = species
  )
) +
  geom_violin()
```
]

.pull-right[
```{r ref.label = "violin", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Ridge plot

.pull-left[
```{r ridge, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g,
    y = species,
    fill = species
  )
) +
  ggridges::geom_density_ridges(alpha = 0.5)
```
]

.pull-right[
```{r ref.label = "ridge", echo = FALSE, warning = FALSE, message = FALSE, out.width = "95%"}
```
]

---

## Ridge plot - more categories + dplyr

.pull-left[
```{r ridge2, fig.show = "hide", warning = FALSE}
penguins %>%
  mutate(
    species_sex = paste0(species, " (", sex, ")")
  ) %>%
  ggplot(
    aes(
      x = body_mass_g,
      y = species_sex,
      fill = species
    )
  ) +
  ggridges::geom_density_ridges(alpha = 0.5)
```
]

.pull-right[
```{r ref.label = "ridge2", echo = FALSE, warning = FALSE, message = FALSE, out.width = "95%"}
```
]

---

## Box plot

.pull-left[
```{r box1, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g,
    y = species
  )
) +
  geom_boxplot()
```
]

.pull-right[
```{r ref.label = "box1", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Box plot - coord_flip

.pull-left[
```{r box2, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = body_mass_g,
    y = species
  )
) +
  geom_boxplot() + 
  coord_flip()
```
]

.pull-right[
```{r ref.label = "box2", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Box plot - swap coords

.pull-left[
```{r box3, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = species,
    y = body_mass_g
  )
) +
  geom_boxplot()
```
]

.pull-right[
```{r ref.label = "box3", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Scatter plot

.pull-left[
```{r scatter1, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm, 
    color = species
  )
) +
  geom_point() 
```
]

.pull-right[
```{r ref.label = "scatter1", echo = FALSE, warning = FALSE, out.width = "95%"}
```
]

---

## Scatter plot - geom_smooth

.pull-left[
```{r scatter3, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm, 
    color = species
  )
) +
  geom_point() +
  geom_smooth(
    fullrange = TRUE
  )
```
]

.pull-right[
```{r ref.label = "scatter3", echo = FALSE, warning = FALSE, out.width = "95%", message=FALSE}
```
]

---

## Scatter plot - geom_smooth w/ lm

.pull-left[
```{r scatter2, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = bill_depth_mm, 
    y = bill_length_mm, 
    color = species
  )
) +
  geom_point() +
  geom_smooth(
    method = "lm",
    se = FALSE,
    fullrange = TRUE
  )
```
]

.pull-right[
```{r ref.label = "scatter2", echo = FALSE, warning = FALSE, out.width = "95%", message=FALSE}
```
]



---

## Line plot

.pull-left[
```{r line, fig.show = "hide", warning = FALSE}
penguins %>%
  count(species, year) %>%
  ggplot(
    aes(
      x = year, 
      y = n, 
      color = species,
      group = species
    )
  ) +
  geom_line()
```
]

.pull-right[
```{r ref.label = "line", echo = FALSE, warning = FALSE, out.width = "95%", message=FALSE}
```
]

---

## Line plot - with points

.pull-left[
```{r line2, fig.show = "hide", warning = FALSE}
penguins %>%
  count(species, year) %>%
  ggplot(
    aes(
      x = year, 
      y = n, 
      color = species,
      group = species
    )
  ) +
  geom_line() + 
  geom_point()
```
]

.pull-right[
```{r ref.label = "line2", echo = FALSE, warning = FALSE, out.width = "95%", message=FALSE}
```
]


---

## Bar plot

.pull-left[
```{r bar1, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = species
  )
) +
  geom_bar()
```
]

.pull-right[
```{r ref.label = "bar1", echo = FALSE, warning = FALSE, out.width = "95%", message=FALSE}
```
]

---

## Stacked bar plot

.pull-left[
```{r bar2, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = species,
    fill = island
  )
) +
  geom_bar()
```
]

.pull-right[
```{r ref.label = "bar2", echo = FALSE, warning = FALSE, out.width = "95%", message=FALSE}
```
]

---

## Stacked relative frequency bar plot

.pull-left[
```{r bar3, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = species,
    fill = island
  )
) +
  geom_bar(position = "fill")
```
]

.pull-right[
```{r ref.label = "bar3", echo = FALSE, warning = FALSE, out.width = "95%", message=FALSE}
```
]

---

## Dodged bar plot

.pull-left[
```{r bar4, fig.show = "hide", warning = FALSE}
ggplot(
  penguins, 
  aes(
    x = species,
    fill = sex
  )
) +
  geom_bar(position = "dodge")
```
]

.pull-right[
```{r ref.label = "bar4", echo = FALSE, warning = FALSE, out.width = "95%", message=FALSE}
```
]

---
class: middle

# Exercises

---

## Exercise 1

Recreate, as faithfully as possible, the following plot using ggplot2 and the `penguins` data.

```{r echo = FALSE, out.width = "60%", fig.height = 5}
penguins %>%
  filter(!is.na(sex)) %>%
  ggplot(
    aes(
      x = body_mass_g,
      fill = species
    ) 
  ) +
  geom_density(alpha = 0.5, color = NA) +
  facet_wrap(~sex, nrow = 2) + 
  labs(
    x = "Body Mass (g)",
    y = "",
    fill = "Species"
  )
```


---

## Exercise 2

Recreate, as faithfully as possible, the following plot from the `palmerpenguin` package readme in ggplot2.

```{r echo = FALSE, out.width="55%"}
knitr::include_graphics("imgs/palmer_plot_ex2.png")
```

.footnote[
`palmerpenguins` pkgdown [site](https://allisonhorst.github.io/palmerpenguins/)
]
