---
title: "Introduction<br>Example"
author: "Keith VanderLinden<br>Calvin University"
output:
  xaringan::moon_reader:
    nature:
      titleSlideClass: ["left", "top"]
      highlightLines: true
      ratio: "16:9"      
---

<!--
Start with a scripture reading that is in keeping with the perspectives
assignments in the course (TBD). Ken uses Philippians 1:9-11.
-->

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

# Data Science in Action

Consider H. Rosling&rsquo;s well-known demonstration of data visualization.

<iframe width="560" height="315" src="https://www.youtube.com/embed/hVimVzgtD6w" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

.footnote[See: https://www.ted.com/talks/hans_rosling_the_best_stats_you_ve_ever_seen]

???
This is Hans Rosling's 2006 TED talk. Rosling died in 2017.

They will have watched this for the preparation assignment. Review the answers to the preparation question on Rosling's students (the problem wasn't so much ignorance and preconceptions). Data tests preconceptions.

Here, we do a single, full example that approximates Rosling's visualization.

References:
- Mine's UN votes (https://www.youtube.com/watch?v=OJ1xR0ObhIw)? 
- H. Rosling's videos (https://youtu.be/Z8t4k0Q8e8Y or https://www.youtube.com/watch?v=hVimVzgtD6w 0:00-5:15) - Ken's example https://cs.calvin.edu/courses/data/202/ka37/slides/w02/w2d2-vis.html

---
# Example: Raw Data

```{r echo = FALSE}
library(gapminder)
library(tidyverse)
```

.pull-left[
```{r gapminder1, results="hide", message=FALSE, warning=FALSE}
gapminder #<<
```
.footnote[See: https://www.rdocumentation.org/packages/gapminder]
]
.pull-right[
```{r ref.label="gapminder1", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8}
```
]

???
This is the raw data, taken from the gapminder library. A tibble is a upgraded R data.frame.
- The dataset is rectangular, as most simple datasets are.
- There are 1704 rows/observations; 6 columns/variables; and, thus, 10224 values (1704*6) 

---
# Example: Data Summary

.pull-left[
```{r gapminder2, results="hide", message=FALSE, warning=FALSE}
gapminder %>% 
  summarize(AvgLifeExp = mean(lifeExp))   #<<
```
]
.pull-right[
```{r ref.label="gapminder2", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8}
```
]

???
This groups the data by year and computes an average per group.

---
# Example: Data Aggregation

.pull-left[
```{r gapminder3, results="hide", message=FALSE, warning=FALSE}
gapminder %>% 
  group_by(year) %>%                      #<<
  summarize(AvgLifeExp = mean(lifeExp))
```
]
.pull-right[
```{r ref.label = "gapminder3", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8}
```
]

???
This groups the data by year and computes an average per group.

---
# Example: Data Visualisation

.pull-left[
```{r gapminder4, fig.show = "hide", message = FALSE, warning = FALSE}
gapminder %>% 
  group_by(year) %>%                      
  summarize(AvgLifeExp = mean(lifeExp)) %>%
  ggplot() + #<<
  aes(x=year, y=AvgLifeExp) +  #<<
  geom_line() #<<
```
]
.pull-right[
```{r ref.label = "gapminder4", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8}
```
]

???
This takes the same data, grouped by year, and plots the average over time using a line plot.

---
# Example: Data Visualisation (cf. Rosling)

.pull-left[
```{r gapminder5, fig.show = "hide", message = FALSE, warning = FALSE}
library(scales)

gapminder %>% 
  filter(year == 2007) %>%
  ggplot() +
  aes(x = gdpPercap, y = lifeExp) +
  geom_point(alpha = .8) +
  aes(color = continent) +
  aes(size = pop) + 
  scale_x_continuous( 
    breaks = c(400, 4000, 40000),
    trans = "log10") +
  labs(x = "GDP per Capita") +
  labs(y = "Life Expectancy (years)") + 
  labs(color = "Continent") +
  labs(size = "Population") +
  scale_size_area(labels=label_comma()) + 
  theme_bw() +
  annotation_logticks(sides = "b")
```
]
.pull-right[
```{r ref.label = "gapminder5", echo = FALSE, warning = FALSE, out.width = "100%", fig.width = 8}
```
]

???
This graph approximates one time-stop of Rosling's famous animation.

Summarize this only; we'll demo the code detail in the RStudio slides.
