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.
gap_1997 <- gapminder %>%
filter(year == 1997)least_americas <- gap_1997 %>%
filter(continent == "Americas") %>%
arrange(pop) %>%
slice_min(pop, n = 2) %>%
select(
country,
population = pop,
`life expectancy` = lifeExp
)
least_europe <- gap_1997 %>%
filter(continent == "Europe") %>%
arrange(pop) %>%
slice_min(pop, n = 2) %>%
select(
country,
population = pop,
`life expectancy` = lifeExp
)americas_wide <- least_americas %>%
pivot_longer(cols = c(population, 'life expectancy'),
names_to = "Metric") %>%
pivot_wider(names_from = country, values_from = value) %>%
mutate(Continent = "Americas")
europe_wide <- least_europe %>%
pivot_longer(cols = c(population, 'life expectancy'),
names_to = "Metric") %>%
pivot_wider(names_from = country, values_from = value) %>%
mutate(Continent = "Europe")final_col_order <- c(
"Continent", "Metric",
"Jamaica", "Trinidad and Tobago", "Iceland", "Montenegro"
)
final_table <- cbind(
Metric = americas_wide$Metric,
americas_wide[, c("Jamaica", "Trinidad and Tobago")],
europe_wide[, c("Iceland", "Montenegro")]
)
final_table Metric Jamaica Trinidad and Tobago Iceland Montenegro
1 population 2531311.000 1138101.000 271192.00 692651.000
2 life expectancy 72.262 69.465 78.95 75.445
americas_fix <- americas_wide
missing_in_americas <- setdiff(final_col_order, names(americas_fix))
americas_fix <- americas_fix %>%
select(any_of(final_col_order))
europe_fix <- europe_wide
missing_in_europe <- setdiff(final_col_order, names(europe_fix))
europe_fix <- europe_fix %>%
select(any_of(final_col_order))final_table %>%
gt() %>%
tab_spanner(
label = md("**Americas**"),
columns = c(Jamaica, `Trinidad and Tobago`)
) %>%
tab_spanner(
label = md("**Europe**"),
columns = c(Iceland, Montenegro)
) %>%
fmt_number(
columns = c(Jamaica, `Trinidad and Tobago`, Iceland, Montenegro),
rows = Metric == "population",
decimals = 0,
use_seps = TRUE
) %>%
fmt_number(
columns = c(Jamaica, `Trinidad and Tobago`, Iceland, Montenegro),
rows = Metric == "life expectancy",
decimals = 1,
use_seps = TRUE
) %>%
cols_label(Metric = "") %>%
tab_style(
style = cell_fill(color = "#E5F1FA"),
locations = cells_body(columns = c(Iceland))
) %>%
tab_style(
style = cell_fill(color = "#E5F1FA"),
locations = cells_column_labels(columns = c(Iceland))
) %>%
tab_header(
title = md("🇮🇸 **Iceland**"),
subtitle = "Fewer people, longer lives"
) %>%
tab_source_note(
source_note = md("[Skál!](https://www.islandshotel.is/explore-iceland/blog/icelandic-phrases/)")
)| 🇮🇸 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! | ||||
Submission
To submit your assignment:
- Change the author name to your name in the YAML portion at the top of this document
- Render your document to html and publish it to RPubs.
- Submit the link to your Rpubs document in the Brightspace comments section for this assignment.
- Click on the “Add a File” button and upload your .qmd file for this assignment to Brightspace.
Grading Rubric
| Item (percent overall) |
100% - flawless | 67% - minor issues | 33% - moderate issues | 0% - major issues or not attempted |
|---|---|---|---|---|
| Narrative: typos and grammatical errors (7%) |
||||
| Document formatting: correctly implemented instructions (7%) |
||||
| Accuracy of replicated image as a gt table (78% ) |
||||
| Submitted properly to Brightspace (8%) |
NA | NA | You must submit according to instructions to receive any credit for this portion. |