---
title: "Exercises for session 4"
---

```{r}
#| label: setup
library(ggplot2)
# Emoji will not always render flawlessly with default device
knitr::opts_chunk$set(dev = "ragg_png")
```

## Exercise 4.1: Ink & Paper

We'll work with this plot of the penguins dataset.

```{r}
p <- ggplot(penguins) +
  aes(body_mass, flipper_len, shape = species) +
  geom_point(na.rm = TRUE) +
  labs(
    x = "Body Mass (g)",
    y = "Flipper Length (mm)",
    shape = "Species"
  )
p
```

Pick any of the pre-existing complete themes and make a 'dark mode' variant
of it.

```{r}
p + ______
```

## Exercise 4.2: Default palettes

This plot has a shape scale with the default palette.

```{r}
p <- ggplot(mpg, aes(displ, hwy, shape = fl)) +
  geom_point(size = 5) +
  scale_shape_discrete(
    breaks = c(
      regular = "r",
      ethanol = "e",
      diesel  = "d",
      premium = "p",
      CNG     = "c"
    )
  ) +
  labs(
    x = "Engine Displacement",
    y = "Fuel efficiency",
    shape = "Fuel type"
  )
p
```

Make a discrete shape palette using emoji.
You can pick a theme like 'farm animals', 'bugs', 'flowers' or other symbols.
Set it as the default palette in the `theme()`.

```{r}
my_shape_pal <- ______

p + theme(______ = my_shape_pal)
```

## Exercise 4.3: Build your own theme

We have the following contour plot of eruption measurements taken from the geyser 'Old Faithful'.

```{r}
p <- ggplot(faithful) +
  aes(waiting, eruptions) +
  geom_density2d(bins = 8) +
  geom_point() +
  labs(
    x = "Waiting time (min)",
    y = "Eruption time (min)"
  )
p
```

Pick some topic that has a strong association with a colour scheme.
It can be a forest, the ocean, the desert or something you come up with.
Create your own theme with that topic in mind that:

* Your theme is a function
* Passes arguments to a base theme
* Adjusts layer defaults

```{r}
my_theme <- ______

p + my_theme()
```

## Exercise 4.4: custom guides

We have the following unemployment data from the United States.

```{r}
library(legendry)

p <- ggplot(economics, aes(date, unemploy)) +
  geom_area(colour = "black", alpha = 0.3) +
  labs(
    x = "Date",
    y = "Unemployment (x1000)"
  )

p
```

Pick some event or period in the range of the plot above.
Create an axis annotation to indicate that event or period.
You may need to construct the appropriate `key` argument for a custom guide.

```{r}
my_annotation <- ______

p + guides(x = compose_stack("axis", my_annotation))
```

