Submit this file (after adding your name after “author”) using Canvas. Make sure to label your plots! Then load packages that you will use.

1. Load the library dslabs and define the dataset gapminder as gapminder <- as_tibble(gapminder).

library(dslabs)
library(tidyverse)
data(gapminder)
gapminder <- as_tibble(gapminder)

2. Create a new variable called gdp_per_cap corresponding to gdp divided by population.

library(dplyr)
library(gapminder)
## 
## Attaching package: 'gapminder'
## The following object is masked _by_ '.GlobalEnv':
## 
##     gapminder
## The following object is masked from 'package:dslabs':
## 
##     gapminder
data("gapminder")
gdp <- c(1000,2000,3000,4000,5000)
population <- c(1000, 2000, 3000, 4000, 5000)
gdp_per_cap <- gdp / population

3. Compute the average population per continent per year, call it mean_pop, removing missing values and assigning the output to a new object called gapminder_new.

gapminder_new <- gapminder %>%
    group_by(continent, year) %>%
    summarise(mean_pop = mean(population, na.rm = TRUE))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.

4. Plot the average population over time, using a different color for each continent. Don’t forget to label your axes. (You may want to drop observations for 2016, here or at an earlier point, if you want to avoid a warning saying that there are NA values. This comes from missing values for 2016.)

library(ggplot2)
gapminder_new %>%
    filter(year != 2016) %>%
    ggplot(aes(x = year, y = mean_pop, color = continent)) +
    geom_line() +
    ggtitle("Average Population by Continent and Year") +
    xlab("Year") +
    ylab("Population")

5. Create a histogram of life expectancy in 2014. Within the appropiate geom_* set:

Call this graph g.

library(ggplot2)
g <- gapminder %>%
    filter(year == 2014) %>%
    ggplot(aes(x = lifeExp)) +
    geom_histogram(binwidth = 5, boundary = 45, color = "white") +
    ggtitle("Life Expectancy in 2014") +
    xlab("Life Expectancy (years)") +
    ylab("Frequency")

6. Using the previous graph g, facet it by continent such that each continent’s plot is a new row. (Hint: check for help for facet_grid)

7. Create a scatter plot of fertility rate (y-axis) with respect to infant mortality (x-axis) in 2014. Within the appropiate geom_*, set

library(ggplot2)
g <- gapminder %>%
    filter(year == 2014) %>%
    ggplot(aes(x = infantMortality, y = fertility)) +
    geom_point(size = 3, alpha = 0.5, color = "#009E73") +
    ggtitle("Fertility vs Infant Mortality in 2014") +
    xlab("Infant Mortality") +
    ylab("Fertility Rate")