Skip to content

Commit

Permalink
w3 slides
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Sep 9, 2024
1 parent e17a0a7 commit 459ffbd
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 128 deletions.
Binary file not shown.
Binary file removed class/3-quarto-plotting/3-quarto-plotting.pdf
Binary file not shown.
Binary file modified class/3-quarto-plotting/3-quarto-plotting.zip
Binary file not shown.
108 changes: 86 additions & 22 deletions class/3-quarto-plotting/ggplot2.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ bears <- read_csv(here::here('data', 'bear_killings.csv'))

If you put it at the "top level" inside `ggplot(aes(...))`, the mapping will apply to all levels. For example:

```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
bears %>%
count(month) %>%
ggplot(aes(x = month, y = n)) +
Expand All @@ -43,7 +46,11 @@ bears %>%

In contrast, if you put the `aes()` mapping inside a single geometry layer, it will only apply to that layer. For example, this will cause an error since the `geom_line()` part doesn't have an aesthetic mapping:

```{r, fig.height=4, fig.width=6, error=TRUE}
```{r}
#| fig-height: 4
#| fig-width: 6
#| error: true
bears %>%
count(month) %>%
ggplot() +
Expand All @@ -57,31 +64,43 @@ bears %>%

Basic scatterplot:

```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
mpg %>%
ggplot() +
geom_point(aes(x = displ, y = hwy))
```

Change color for all points:

```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
mpg %>%
ggplot() +
geom_point(aes(x = displ, y = hwy), color = 'blue')
```

To change color based on a variable, map the variable to `color` in `aes()`:

```{r, fig.height=4, fig.width=7}
```{r}
#| fig-height: 4
#| fig-width: 7
mpg %>%
ggplot() +
geom_point(aes(x = displ, y = hwy, color = class))
```

Map the shape instead of color (usually not a great idea):

```{r, fig.height=4, fig.width=7}
```{r}
#| fig-height: 4
#| fig-width: 7
mpg %>%
ggplot() +
geom_point(aes(x = displ, y = hwy, shape = class))
Expand All @@ -93,7 +112,10 @@ What happened to SUV?

`geom_line()` connects all the dots:

```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
mpg %>%
ggplot() +
geom_line(aes(x = displ, y = hwy))
Expand All @@ -103,15 +125,21 @@ The reason this looks messy is because `geom_line()` is trying to literally conn

If you wanted a single "best-fit" trend line, use `geom_smooth()`:

```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
mpg %>%
ggplot() +
geom_smooth(aes(x = displ, y = hwy))
```

Set `se = FALSE` to drop the error bounds:

```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
mpg %>%
ggplot() +
geom_smooth(aes(x = displ, y = hwy), se = FALSE)
Expand All @@ -128,7 +156,10 @@ mpg %>%

Basic bar plot of the counts:

```{r, fig.height=4, fig.width=7}
```{r}
#| fig-height: 4
#| fig-width: 7
mpg %>%
count(class) %>%
ggplot() +
Expand All @@ -137,7 +168,10 @@ mpg %>%

Re-order bars based on count using `reorder()`:

```{r, fig.height=4, fig.width=7}
```{r}
#| fig-height: 4
#| fig-width: 7
mpg %>%
count(class) %>%
ggplot() +
Expand All @@ -146,7 +180,10 @@ mpg %>%

To change the color for all bars, use `fill` (not `color`):

```{r, fig.height=4, fig.width=7}
```{r}
#| fig-height: 4
#| fig-width: 7
mpg %>%
count(class) %>%
ggplot() +
Expand All @@ -155,7 +192,10 @@ mpg %>%

To change color based on a variable, map the variable to `fill` in `aes()`:

```{r, fig.height=4, fig.width=7}
```{r}
#| fig-height: 4
#| fig-width: 7
mpg %>%
count(class, drv) %>% # Note I had to include drv in the count too
ggplot() +
Expand All @@ -164,7 +204,10 @@ mpg %>%

Use `position = 'dodge'` to change from stacked to side-by-side:

```{r, fig.height=4, fig.width=8}
```{r}
#| fig-height: 4
#| fig-width: 8
mpg %>%
count(class, drv) %>% # Note I had to include drv in the count too
ggplot() +
Expand All @@ -175,17 +218,23 @@ mpg %>%

# Practice

```{r, fig.height=4, fig.width=7}
```{r}
#| fig-height: 4
#| fig-width: 7
```


```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
```


```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
```

Expand All @@ -197,15 +246,21 @@ Facets make multiple small charts and are useful when you have many levels in a

For example, this plot has too many color categories for the color to be useful:

```{r, fig.height=4, fig.width=7}
```{r}
#| fig-height: 4
#| fig-width: 7
mpg %>%
ggplot(aes(x = displ, y = hwy)) +
geom_point(aes(color = class))
```

Instead, we can use `facet_wrap()` to show multiple charts of each vehicle class:

```{r, fig.height=9, fig.width=10}
```{r}
#| fig-height: 9
#| fig-width: 10
mpg %>%
ggplot(aes(x = displ, y = hwy)) +
geom_point() +
Expand All @@ -214,7 +269,10 @@ mpg %>%

You can also use `facet_grid()` to facet by two variables:

```{r, fig.height=7, fig.width=10}
```{r}
#| fig-height: 7
#| fig-width: 10
mpg %>%
ggplot(aes(x = displ, y = hwy)) +
geom_point() +
Expand All @@ -225,12 +283,18 @@ mpg %>%

# Extra Practice

```{r, fig.height=5, fig.width=7}
```{r}
#| fig-height: 5
#| fig-width: 7
bears %>%
count(year, gender)
```

```{r, fig.height=4, fig.width=6}
```{r}
#| fig-height: 4
#| fig-width: 6
mpg %>%
mutate(manufacturer = str_to_title(manufacturer)) %>%
group_by(manufacturer) %>%
Expand Down
Loading

0 comments on commit 459ffbd

Please sign in to comment.