---
title: "Data wrangling exercise"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(tidyverse)
library(ggrepel)
```

# Data

```{r}
library(gapminder)
#?gapminder
```

Read about the data.

# Exercises

What years are represented in the data?

```{r}

```

What countries starting with "D" are represented in the data?

```{r}

```


What are the average `lifeExp`'s for the countries starting with "D" over all the years represented in the data?

```{r}

```


Plot the `lifeExp`'s for the countries starting with "D" using `ggplot()` and `geom_line()` coloured by `country`:

```{r}

```

Plot the `lifeExp`'s for each country using `ggplot()` and `geom_line()` coloured by `continent` (hint: `group`):

```{r}

```

Same, but with line opacity and size given by population size, `pop`:

```{r}

```

What countries have the highest mean population size (hint: `arrange`/`desc`)?

```{r}

```

What is the mean population size for each year and continent?

```{r}

```

What is the mean population size for each decade and continent?

```{r}

```


...and in wide format with continent as rows and decade as columns?

```{r}

```

What countries have the highest deviation of life expectancy from a linear trend and how would you define such a deviation (can you come up with more definitions?)?

```{r}

```

... and illustrate it:

```{r}

```



Convert `gapminder` data to long format from columns `lifeExp`, `pop`, and `gdpPercap` (the new variables can be called anything, e.g. `Measure` and `Value`):

```{r}

```

...use these to construct a facetted line plot (with 1 column) coloured by continent with `Value` for each `year` with a facet for each `Measure` (remember `group`; also take a look at the `scales` parameter to the `facet_wrap` function):

```{r}

```

It seems like one country's `gdpPercap` was extremely high in the 1950's and then decreased rapidly in the 1970's. What country is that?

```{r}

```


Let us annotate the plot with the names of the top-5 biggest countries measured by mean population size over the years (hint: `ggrepel` package and `geom_label_repel`):

Try to make the plot as beautiful as possible (inspiration available at `day-3-data-wrangling-exercises-top5.png`).

```{r}

```


