---
title: "R Markdown documents are awesome"
author: "Insert your name here"
date: "`r Sys.Date()`"
output:
  html_document: default
  pdf_document: default
  word_document: default 
---

# Introduction

Welcome to R Markdown! This is your working document. Follow along with the instructions in Week 6: Topic 2.

```{r setup, include=FALSE}
# This chunk can be used to set some defaults for the whole document
knitr::opts_chunk$set(echo = TRUE)
```

# Data and set-up

Loading the packages:

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

We are using the gapminder dataset:

```{r}
mydata <- gapminder %>% 
  # round gdpPercap to 0 decimals:
  mutate(gdpPercap = round(gdpPercap)) %>% 
  # divide pop by million, round to 1 decimal:
  mutate(pop_millions = (pop/1e6) %>% 
           round(1))
```

# Table

A random sample of rows in the dataset:

```{r}
mydata %>% 
  select(-pop) %>% 
  sample_n(10) %>% 
  knitr::kable(col.names = c("Country",
                      "Continent",
                      "Year",
                      "Life Expectancy",
                      "GPD per Capita",
                      "Population (millions"))
```

Number of variables: `r mydata %>% ncol()`.

Total number of observations: `r mydata %>% nrow()`.

# Plotting

```{r}
mydata %>% 
  filter(year == 2007) %>% 
  ggplot(aes(x = gdpPercap/1000, #divide by 1000 to tidy the x-axis
             y = lifeExp,
             colour = continent,
             size = pop_millions)) +
  geom_point(shape = 1) +
  labs(y = "Life Expectancy", x = "GDP per Capita/1000", 
       colour = "Contintent", size = "Population in Millions") + 
  theme_bw()
```
