---
title: "Lab 4.1 - Plotting with `ggplot2`"
author: "Author Goes Here"
date: "Semester Goes Here"
output:
  html_document:
    code_download: true
---

This document is a record of some basic ggplot2 exercises.

```{r setup, message=FALSE}
library(tidyverse)
library(gapminder)

# Put the data in the Environment
gapminder <- gapminder::gapminder

# Extract the data for the latest year in the dataset, for plots that don't involve year.
gapminder_latest <- filter(gapminder, year == 2007)
```

🚧
After running the `setup` code chunk, you should have two tibbles, both listed in the "Environment" tab. Name and describe those tibbles here. You may know more about their value when 're finished with the lab, so write what you can now and come back later and fill in the details.

## Styling and labeling plots

🚧
Imagine that you're trying to understand the life expectancy in the US over time, and modify the following plot to:

1. Add labels (`labs`) for the x and y axes. Use `?gapminder` at the Console to get the data documentation if needed.
2. Add a title (also `labs`).
3. Set the color of the line to green. (Is it green now?)
4. Set the chunk name to something more meaningful than `plot-1`.

Finally, add a short explanation of what the visualization suggests.

```{r plot-1}
gapminder %>% 
  filter(country == "United States") %>% 
  ggplot() +
  aes(x = year, y = lifeExp) +
  geom_line()
```

## Mapping vs Faceting

🚧
In the following plot, notice that we're `filter`ing to only include rows where the `country` is in the list of `countries` that we made. This is so that we don't have `r length(unique(gapminder$country))` different colors or facets in these graphs.

Imagine that you're trying to understand the life expectancy in these countries over time, and then do the following:

1. Run the plot as specified. Why is it shaped like this? (If you're not sure, click `gapminder` in  the Environment pane and think about how you'd draw a line with that data.)
2. Map `color` to `country`. How has the plot changed?
3. Facet by country. Is this better than the color mapping?
4. Setting `scales = "free_y"` in the faceting function. Is this better?

When finished, set your plot to the most useful form given the goal stated above. Add an explanation of what the visualization suggests.

```{r plot-2}
countries <- c(
  "China", "India", "United States",
  "Indonesia", "Brazil", "Pakistan",
  "Bangladesh", "Nigeria", "Japan")

gapminder %>% 
  filter(country %in% countries) %>% 
  ggplot() +
  aes(x = year, y = lifeExp) +
  geom_line()
```

## Univariate analysis --- Continuous

🚧
Pick one of the continuous variables and pose a question you'd like to answer. Then, make a plot that shows its distribution and help answer your question. 

Include your plot and a description of what it suggests with respect to your initial question.

## Univariate Analysis --- Categorical

🚧
Imagine that you'd like to know which continent has the most countries. Compose a plot that helps to answer this question. (Use `gapminder_latest`, which has one row per country.)

Include your plot and a description of what it suggests with respect to the given question.

## Bivariate Analysis --- Numerical/Categorical

🚧
Pick one of the continuous variables and set a question you'd like to answer about that variable with respect to the continents. If possible, choose a variable you haven't chosen before. Compose a plot that helps you answer your question.

Include your plot and a description of what it suggests with respect to your initial question.
