library(tidyverse)
library(gapminder)
library(gt)
library(gtExtras)Assignment 5
Go to the shared posit.cloud workspace for this class and open the assign05 project. Open the assign05.qmd file and complete the exercises.
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.
The Grading Rubric is available at the end of this document. Note: you will receive deductions for not using tidyverse syntax in this assignment. That includes the use of filter, mutate, and the up-to-date pipe operator |>.
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.
```{# Load required libraries} library(tidyverse) library(gapminder) library(gt) library(gtExtras) library(janitor) # for cleaning column names
Step 1: Filter the necessary countries and year
gap_1997 <- gapminder |> filter(year == 1997, continent %in% c(“Americas”, “Europe”)) |> filter(country %in% c(“Jamaica”, “Trinidad and Tobago”, “Iceland”, “Montenegro”))
library(tidyverse)
library(gapminder)
gap_1997 <- gapminder |>
filter(year == 1997, continent %in% c("Americas", "Europe")) |>
filter(country %in% c("Jamaica", "Trinidad and Tobago", "Iceland", "Montenegro"))
print(gap_1997)# A tibble: 4 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Iceland Europe 1997 79.0 271192 28061.
2 Jamaica Americas 1997 72.3 2531311 7122.
3 Montenegro Europe 1997 75.4 692651 6466.
4 Trinidad and Tobago Americas 1997 69.5 1138101 8793.
Step 2: Select relevant columns using correct column names (pop for population)
gap_data <- gap_1997 |> select(all_of(c(“country”, “continent”, “pop”, “lifeExp”)))
gap_data <- gap_1997 |>
select(all_of(c("country", "continent", "pop", "lifeExp")))
print(gap_data)# A tibble: 4 × 4
country continent pop lifeExp
<fct> <fct> <int> <dbl>
1 Iceland Europe 271192 79.0
2 Jamaica Americas 2531311 72.3
3 Montenegro Europe 692651 75.4
4 Trinidad and Tobago Americas 1138101 69.5
Step 3: Use pivot_wider() to reshape the data, creating unique column names
gap_wide <- gap_data |> pivot_wider(names_from = country, values_from = c(pop, lifeExp), names_sep = “_”) |> clean_names() # Clean column names to ensure consistency
library(tidyr)
library(janitor)
Attaching package: 'janitor'
The following objects are masked from 'package:stats':
chisq.test, fisher.test
gap_wide <- gap_data |>
pivot_wider(
names_from = country,
values_from = c(pop, lifeExp),
names_sep = "_"
) |>
clean_names()
print(gap_wide)# A tibble: 2 × 9
continent pop_iceland pop_jamaica pop_montenegro pop_trinidad_and_tobago
<fct> <int> <int> <int> <int>
1 Europe 271192 NA 692651 NA
2 Americas NA 2531311 NA 1138101
# ℹ 4 more variables: life_exp_iceland <dbl>, life_exp_jamaica <dbl>,
# life_exp_montenegro <dbl>, life_exp_trinidad_and_tobago <dbl>
Check column names to verify the structure
column_names <- colnames(gap_wide) print(column_names) # This will display the column names
column_names <- colnames(gap_wide)
print(column_names) [1] "continent" "pop_iceland"
[3] "pop_jamaica" "pop_montenegro"
[5] "pop_trinidad_and_tobago" "life_exp_iceland"
[7] "life_exp_jamaica" "life_exp_montenegro"
[9] "life_exp_trinidad_and_tobago"
Step 4: Create the gt table
gap_wide |> gt() |> tab_header(title = md(“Iceland”), subtitle = md(“Fewer people, longer lives”)) |> tab_spanner(label = “Americas”, columns = c(pop_jamaica, pop_trinidad_and_tobago)) |> tab_spanner(label = “Europe”, columns = c(pop_iceland, pop_montenegro)) |> cols_label( pop_jamaica = “Jamaica”, pop_trinidad_and_tobago = “Trinidad and Tobago”, pop_iceland = “Iceland”, pop_montenegro = “Montenegro”, life_exp_jamaica = “72.3”, life_exp_trinidad_and_tobago = “69.5”, life_exp_iceland = “79.0”, life_exp_montenegro = “75.4” ) |> tab_source_note(md(“Skál!”)).
library(gt)
gt_table <- gap_wide |>
gt() |>
tab_header(
title = md("🇮🇸 **Iceland**"),
subtitle = md("Fewer people, longer lives")
) |>
tab_spanner(
label = "Americas",
columns = c(pop_jamaica, pop_trinidad_and_tobago)
) |>
tab_spanner(
label = "Europe",
columns = c(pop_iceland, pop_montenegro)
) |>
cols_label(
pop_jamaica = "Jamaica",
pop_trinidad_and_tobago = "Trinidad and Tobago",
pop_iceland = "Iceland",
pop_montenegro = "Montenegro",
life_exp_jamaica = "72.3",
life_exp_trinidad_and_tobago = "69.5",
life_exp_iceland = "79.0",
life_exp_montenegro = "75.4"
) |>
tab_source_note(
md("Skál!")
)
gt_table🇮🇸 Iceland |
||||||||
|---|---|---|---|---|---|---|---|---|
Fewer people, longer lives |
||||||||
| continent | Europe | Americas | 79.0 | 72.3 | 75.4 | 69.5 | ||
| Iceland | Montenegro | Jamaica | Trinidad and Tobago | |||||
| Europe | 271192 | 692651 | NA | NA | 78.95 | NA | 75.445 | NA |
| Americas | NA | NA | 2531311 | 1138101 | NA | 72.262 | NA | 69.465 |
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. |