---
title: "Visualizing data with ggplot"
format: html
---

```{r}
#| label: setup
#| warning: false
#| message: false

library(tidyverse)  # For ggplot, dplyr, and friends

mpg <- read_csv("data/mpg.csv")  # Load data
```

## Figuring out what's in the data

```{r}
mpg
```

## Your turn #1

Run the code from the the slide to make a graph.

```{r}

```

## Your turn #2: ggplot playground

Add `color`, `size`, `alpha`, or `shape` aesthetics to your graph. Experiment.  

```{r}
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))
```

## Your turn #3: Switching geoms

Replace this scatterplot with one that draws boxplots. Use the cheatsheet. Try your best guess.

```{r}
ggplot(data = mpg) +
  geom_point(mapping = aes(x = class, y = hwy))
```

## Your turn #4: Histograms

Make a histogram of the `hwy` variable from `mpg`.

```{r}
ggplot(data = mpg)
```

## Your turn #5: Density plots

Make a density plot `hwy` colored by `class`.

```{r}
ggplot(data = mpg) 
```

## Your turn #6: Complex plots

Predict what this code will do. Then run it.

```{r}
ggplot(mpg) + 
  geom_point(aes(displ, hwy)) +
  geom_smooth(aes(displ, hwy))
```

---

## Fancy completed plot from slides

```{r}
#| fig-width: 6
#| fig-height: 6
#| message: false

ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
  geom_point() +
  geom_smooth(method = "lm") + 
  scale_color_viridis_d() +
  facet_wrap(vars(drv), ncol = 1) +
  labs(x = "Displacement", y = "Highway MPG",
       color = "Drive", 
       title = "Heavier cars get lower mileage",
       subtitle = "Displacement indicates weight(?)",
       caption = "I know nothing about cars") +
  theme_bw() +
  theme(legend.position = "bottom",
        plot.title = element_text(face = "bold"))
```

---

## Takeaways

You can use this code template to any kind of graph with **ggplot2**.

```{r eval=FALSE}
ggplot(data = <DATA>) +
  <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
```
