1
library(gapminder)
library(tidyverse)## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(skimr)
library(ggthemes)skim(gapminder)| Name | gapminder |
| Number of rows | 1704 |
| Number of columns | 6 |
| _______________________ | |
| Column type frequency: | |
| factor | 2 |
| numeric | 4 |
| ________________________ | |
| Group variables | None |
Variable type: factor
| skim_variable | n_missing | complete_rate | ordered | n_unique | top_counts |
|---|---|---|---|---|---|
| country | 0 | 1 | FALSE | 142 | Afg: 12, Alb: 12, Alg: 12, Ang: 12 |
| continent | 0 | 1 | FALSE | 5 | Afr: 624, Asi: 396, Eur: 360, Ame: 300 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| year | 0 | 1 | 1979.50 | 17.27 | 1952.00 | 1965.75 | 1979.50 | 1993.25 | 2007.0 | ▇▅▅▅▇ |
| lifeExp | 0 | 1 | 59.47 | 12.92 | 23.60 | 48.20 | 60.71 | 70.85 | 82.6 | ▁▆▇▇▇ |
| pop | 0 | 1 | 29601212.32 | 106157896.74 | 60011.00 | 2793664.00 | 7023595.50 | 19585221.75 | 1318683096.0 | ▇▁▁▁▁ |
| gdpPercap | 0 | 1 | 7215.33 | 9857.45 | 241.17 | 1202.06 | 3531.85 | 9325.46 | 113523.1 | ▇▁▁▁▁ |
glimpse(gapminder)## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
Country and continent are categorical variables. The rest of the variables are quantitative.
Country is a factor and categorical variable that tells us the country of the observation.
Continent is a factor and categorical variable that describes the continent of the observation.
Year is an integer and quantitative variable that describes the year of the observation.
lifExp is a double and quantitative variable that describes the average life expectancy upon birth of the observation.
pop is an integer and quantitative variable that describes the total population of the given observation.
gdpPercap is a double and quantitative variable that describes the per-capita GDP of the given observation.
There are no missing values from the dataset.
Scatterplot showing life expectancy across time.
gapminder %>% ggplot(aes(x = year,
y = lifeExp)) +
geom_point() +
labs(x = "Year",
y = "Average Life Expectancy Upon Birth",
title = "Average Life Expectancy Over the Years",
caption = "Data Source: Gapminder data set from gapminder package.") Life expectancy seems to on average increase over the years.
gapminder %>% ggplot(aes(x = year,
y = lifeExp,
color = continent)) +
geom_point() +
theme_minimal() +
facet_grid(. ~ continent)+
geom_smooth(se = FALSE) +
labs(x = "Year",
y = "Life Expectancy (Years)",
title = "Average Life Expectancy Over the Years",
caption = "Data Source: Gapminder data set from gapminder package.") +
scale_colour_colorblind() +
theme(axis.text.x = element_text(angle = +45),
legend.position = "none") ## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Oceania has the highest life expectancy on average.
2
gapminder2007 <- gapminder %>% filter(year == 2007) %>% slice_max(pop, n = 20)gapminder2007 %>% ggplot(aes(x = country,
y = pop)) +
geom_col()gapminder2007 %>% ggplot(aes(x = fct_reorder(country, pop),
y = pop)) +
geom_col()gapminder2007 %>% ggplot(aes(x = fct_reorder(country, pop),
y = pop,
fill = continent)) +
theme_solarized() +
geom_col(color = "black") +
coord_flip() +
theme(legend.position = "bottom",
legend.title = element_blank()) +
labs(x = "Country",
y = "Total Population",
title = "World's most populated countries, 2007",
caption = "Data Source: Gapminder dataset from gapminder package")