HW5-Data110

Author

Jacob Dawson

Published

March 14, 2025

library(scales) #loading libraries
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.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ readr::col_factor() masks scales::col_factor()
✖ purrr::discard()    masks scales::discard()
✖ 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
library(highcharter)
Warning: package 'highcharter' was built under R version 4.4.3
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
library(RColorBrewer)
setwd("C:/Users/Jacob/Downloads")
nations <- read_csv("nations(1).csv") |> #mutating for gdp in trillions
  mutate(gdp_tn = 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.
head(nations)
# 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 AD    AND   Andorra  1996         NA      64291       10.9                2.8
2 AD    AND   Andorra  1994         NA      62707       10.9                3.2
3 AD    AND   Andorra  2003         NA      74783       10.3                2  
4 AD    AND   Andorra  1990         NA      54511       11.9                4.3
5 AD    AND   Andorra  2009         NA      85474        9.9                1.7
6 AD    AND   Andorra  2011         NA      82326       NA                  1.6
# ℹ 3 more variables: region <chr>, income <chr>, gdp_tn <dbl>
big4 <- nations |>
  filter(iso3c %in% c("CHN","DEU", "JPN", "USA")) |> #filtering for countries
  arrange(year)
head(big4)
# 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 DE    DEU   Germany   1990     19033.   79433029       11.4                3.4
3 JP    JPN   Japan     1990     19230.  123537000       10                  2.5
4 US    USA   United …  1990     23954.  249623000       16.7                5.8
5 CN    CHN   China     1991      1091. 1150780000       19.7               29.7
6 DE    DEU   Germany   1991     20521.   80013896       10.4                3.5
# ℹ 3 more variables: region <chr>, income <chr>, gdp_tn <dbl>
ggplot(big4, aes(x=year, y=gdp_tn, color = country)) + #First chart
  geom_point() +
  geom_line() +
  labs(title = "China's Rise to Become the Largest Economy",
       y = "GDP(Trillions of $)",
       x = "Year") +
  scale_color_brewer(palette = "Set1") +
  theme_minimal()

regions <- nations |> #arranging data for regions
  group_by(year,region) |>
  summarize(gdp_tn = sum(gdp_tn, na.rm = TRUE)) |>
  arrange(year,region)
`summarise()` has grouped output by 'year'. You can override using the
`.groups` argument.
head(regions)
# A tibble: 6 × 3
# Groups:   year [1]
   year region                     gdp_tn
  <dbl> <chr>                       <dbl>
1  1990 East Asia & Pacific          5.52
2  1990 Europe & Central Asia        9.36
3  1990 Latin America & Caribbean    2.40
4  1990 Middle East & North Africa   1.66
5  1990 North America                6.54
6  1990 South Asia                   1.35
ggplot(regions, aes(x=year, y=gdp_tn, fill = region)) + #second chart
  geom_area() +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "GDP by World Bank Region",
       x = "Year",
       y = "GDP($ Trillions)") +
  theme_minimal()