Nations Two Charts

Author

Gabriel Castillo

Library Section

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ 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
library(ggplot2)
library(RColorBrewer)

Loading in the data

setwd("C:/Users/casti/OneDrive/Documents/DATA 110")
Nations_Data <- read_csv("nations.csv")
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.

New variable GDP

Nations_Data <- Nations_Data |> 
  mutate(GDP = (gdp_percap * population)/10^12)
Nations_Data
# A tibble: 5,275 × 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
 7 AD    AND   Andorra  2004         NA      78337       10.9                2  
 8 AD    AND   Andorra  2010         NA      84419        9.8                1.7
 9 AD    AND   Andorra  2001         NA      67770       11.8                2.1
10 AD    AND   Andorra  2002         NA      71046       11.2                2.1
# ℹ 5,265 more rows
# ℹ 3 more variables: region <chr>, income <chr>, GDP <dbl>

Data for first chart

Chart1 <- Nations_Data |> 
  group_by(country) |> 
  filter(country == "China"|country == "Germany"| country == "United States"| country == "Japan") 
Chart1 |> 
  ggplot(aes(x=year, y = GDP, fill = country, color = country)) +
  labs(title = "China's Rise to Become the Largest Economy", y = " GDP ($ trillion)") +
  geom_line() +
  geom_point() +
  scale_color_brewer(palette = "Set1") +
  theme_minimal(base_size = 14)

Getting the Data for Chart 2

chart2 <- Nations_Data |>
  group_by(region,year) |>
  summarise(GDP = sum(GDP, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.

Making Chart 2

chart2 |>
  ggplot( aes(x= year , y = GDP , fill = region)) +
  geom_line() +
  geom_area(color = "white") +
  labs(title = "GDP by World Bank Region", y = "GDP ($ trillion)") + 
  scale_fill_brewer(palette = "Set2") + 
  theme_minimal(base_size = 14)