Nations Charts Assignment

Author

Samuel Goon

Load Libraries and creating gdp_tril from gdp_percap and population

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
setwd("C:/Users/samue/Desktop/School Apps/RStudio/RStudio Datasets")
nations <- read_csv("nations.csv") %>%
   mutate(gdp_tril = gdp_percap*population/10^12)
Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Preparing the data for the first chart

four_countries <- nations %>%
 filter(iso3c == "CHN" | iso3c == "JPN" | iso3c == "VNM" | iso3c == "USA") %>%
  arrange(year)
head(four_countries)
# A tibble: 6 × 11
  iso2c iso3c country   year gdp_percap population birth_rate neonat_mortal_rate
  <chr> <chr> <chr>    <dbl>      <dbl>      <dbl>      <dbl>              <dbl>
1 CN    CHN   China     1990       980. 1135185000       21.1               29.7
2 JP    JPN   Japan     1990     19230.  123537000       10                  2.5
3 US    USA   United …  1990     23954.  249623000       16.7                5.8
4 VN    VNM   Vietnam   1990       970.   66016700       28.7               23.9
5 CN    CHN   China     1991      1091. 1150780000       19.7               29.7
6 JP    JPN   Japan     1991     20467.  123921000        9.9                2.5
# ℹ 3 more variables: region <chr>, income <chr>, gdp_tril <dbl>

Creating the first chart of my four selected countries

ggplot(four_countries, aes(x = year, y = gdp_tril, color = country)) +
  geom_point() +
  geom_line() +
  scale_fill_brewer(palette = "Set1")

  labs(x = "Year",
    y = "GDP ($ Trillion)",
    title = "China's rise to Become the Largest Economy")
$x
[1] "Year"

$y
[1] "GDP ($ Trillion)"

$title
[1] "China's rise to Become the Largest Economy"

attr(,"class")
[1] "labels"

Preparing data for the second chart

region_gdp <- nations %>%
  group_by(region, year) %>%
  summarise(gdp_tril = sum(gdp_tril, na.rm = TRUE)) %>%
  arrange(year, region)
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.

Creating the second chart

ggplot(region_gdp) +
  geom_area(aes(x = year, y = gdp_tril, fill = region)) +
  scale_fill_brewer(palette = "Set2") +
  labs(x = "Year",
       y = "GDP ($ Trillion)",
       title = "GDP by World Bank Region")