library(tidyverse)
library(gapminder)
library(gt)
library(gtExtras)Assignment 5
As best you can, recreate this table showing the population and life expectancy for the two least populated countries in the Americas and Europe for 1997. Your assignment must show all code in the output. Use as many code chunks as you feel necessary, I recommend first getting the output to match the output below. Hint: you will probably find it easiest to use two pivot_wider() functions with a union() to replicate the data layout. Use the gapminder dataset.

The Grading Rubric is available at the end of this document.
The word Skál! should link to https://www.islandshotel.is/explore-iceland/blog/icelandic-phrases/ it doesn’t in the image because it is an image. The following packages are useful and loaded for you.
This is your work area. Add as many code cells as you need.
pop_data <- gapminder |>
filter(country %in% c("Jamaica", "Trinidad and Tobago", "Iceland", "Montenegro"),
year == 1997) |>
select(country, population = pop) |>
mutate(metric = "population") |>
pivot_wider(names_from = country, values_from = population)
pop_data# A tibble: 1 × 5
metric Iceland Jamaica Montenegro `Trinidad and Tobago`
<chr> <int> <int> <int> <int>
1 population 271192 2531311 692651 1138101
life_exp_data <- gapminder |>
filter(country %in% c("Jamaica", "Trinidad and Tobago", "Iceland", "Montenegro"),
year == 1997) |>
select(country, lifeExp) |>
mutate(metric = "life expectancy") |>
pivot_wider(names_from = country, values_from = lifeExp)
life_exp_data# A tibble: 1 × 5
metric Iceland Jamaica Montenegro `Trinidad and Tobago`
<chr> <dbl> <dbl> <dbl> <dbl>
1 life expectancy 79.0 72.3 75.4 69.5
data_join <- union(pop_data, life_exp_data) |>
select(metric, Jamaica, `Trinidad and Tobago`, Iceland, Montenegro) |>
mutate(across(c(Jamaica, `Trinidad and Tobago`, Iceland, Montenegro),
~ ifelse(metric == "population",
scales::comma(round(.x, 0)),
sprintf("%.1f", round(.x, 1)))))
data_join# A tibble: 2 × 5
metric Jamaica `Trinidad and Tobago` Iceland Montenegro
<chr> <chr> <chr> <chr> <chr>
1 population 2,531,311 1,138,101 271,192 692,651
2 life expectancy 72.3 69.5 79.0 75.4
data_join <- data_join |>
gt() |>
tab_spanner(
label = md("**Europe**"),
columns = c('Iceland', 'Montenegro')) |>
tab_spanner(
label = md("**Americas**"),
columns = c(Jamaica, `Trinidad and Tobago`))
data_join| metric |
Americas
|
Europe
|
||
|---|---|---|---|---|
| Jamaica | Trinidad and Tobago | Iceland | Montenegro | |
| population | 2,531,311 | 1,138,101 | 271,192 | 692,651 |
| life expectancy | 72.3 | 69.5 | 79.0 | 75.4 |
data_join <- data_join |>
cols_label(
metric = fontawesome::fa("chart-bar"),
Iceland = md("**Iceland**"),
Montenegro = md("**Montenegro**"),
Jamaica = md("**Jamaica**"),
'Trinidad and Tobago' = md("**Trinidad and Tobago**"))
data_join
Americas
|
Europe
|
|||
|---|---|---|---|---|
| Jamaica | Trinidad and Tobago | Iceland | Montenegro | |
| population | 2,531,311 | 1,138,101 | 271,192 | 692,651 |
| life expectancy | 72.3 | 69.5 | 79.0 | 75.4 |
data_join <- data_join |>
tab_header(title = md("🇮🇸**Iceland**"),
subtitle = md("**Fewer people, longer lives**")) |>
tab_style(style = cell_text(align = "left"),
locations = cells_title(groups = c("title", "subtitle"))) |>
tab_source_note(md("[Skál!](https://www.islandshotel.is/explore-iceland/blog/icelandic-phrases/)"))
data_join| 🇮🇸Iceland | ||||
|---|---|---|---|---|
| Fewer people, longer lives | ||||
Americas
|
Europe
|
|||
| Jamaica | Trinidad and Tobago | Iceland | Montenegro | |
| population | 2,531,311 | 1,138,101 | 271,192 | 692,651 |
| life expectancy | 72.3 | 69.5 | 79.0 | 75.4 |
| Skál! | ||||
data_join1 <- data_join |>
gt_theme_guardian() |>
opt_stylize(1) |>
tab_style(style = cell_fill(color = "#9ecae1"),
locations = cells_body(columns = Iceland))
data_join1