Nations GDP Visualization

Author

George Bothos

Loading and exploring the Nations Dataset

# Loading the tidyverse package first for data analysis and visualization
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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
# Now going to import the Nations dataset
nations <- readr::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.
# We will preview the data so we can examine its structure
head(nations)
# A tibble: 6 × 10
  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
# ℹ 2 more variables: region <chr>, income <chr>
names(nations)
 [1] "iso2c"              "iso3c"              "country"           
 [4] "year"               "gdp_percap"         "population"        
 [7] "birth_rate"         "neonat_mortal_rate" "region"            
[10] "income"            
str(nations)
spc_tbl_ [5,275 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ iso2c             : chr [1:5275] "AD" "AD" "AD" "AD" ...
 $ iso3c             : chr [1:5275] "AND" "AND" "AND" "AND" ...
 $ country           : chr [1:5275] "Andorra" "Andorra" "Andorra" "Andorra" ...
 $ year              : num [1:5275] 1996 1994 2003 1990 2009 ...
 $ gdp_percap        : num [1:5275] NA NA NA NA NA NA NA NA NA NA ...
 $ population        : num [1:5275] 64291 62707 74783 54511 85474 ...
 $ birth_rate        : num [1:5275] 10.9 10.9 10.3 11.9 9.9 NA 10.9 9.8 11.8 11.2 ...
 $ neonat_mortal_rate: num [1:5275] 2.8 3.2 2 4.3 1.7 1.6 2 1.7 2.1 2.1 ...
 $ region            : chr [1:5275] "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" ...
 $ income            : chr [1:5275] "High income" "High income" "High income" "High income" ...
 - attr(*, "spec")=
  .. cols(
  ..   iso2c = col_character(),
  ..   iso3c = col_character(),
  ..   country = col_character(),
  ..   year = col_double(),
  ..   gdp_percap = col_double(),
  ..   population = col_double(),
  ..   birth_rate = col_double(),
  ..   neonat_mortal_rate = col_double(),
  ..   region = col_character(),
  ..   income = col_character()
  .. )
 - attr(*, "problems")=<pointer: 0x00000131adffff70> 

Calculating the GDP in trillions

# Calculate each country's GDP in trillions
nations <- nations %>%
  mutate(gdp = (gdp_percap * population) / 10^12)

# Let's preview the new GDP variable
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 <dbl>

Filtering the dataset so that it’s four countries

# We are keeping only China, Germany, Japan, and the United States
nations_four <- nations %>%
  filter(country %in% c("China", "Germany", "Japan", "United States"))

# Checking the countries and also previewing their GDP values
unique(nations_four$country)
[1] "China"         "Germany"       "Japan"         "United States"
head(nations_four %>% select(country, year, gdp))
# A tibble: 6 × 3
  country  year   gdp
  <chr>   <dbl> <dbl>
1 China    1992  1.47
2 China    2005  6.59
3 China    2000  3.68
4 China    1991  1.26
5 China    2013 16.6 
6 China    1999  3.32

Comparing the GDP of the four countries

# Making a line chart to compare GDP across four countries
ggplot(nations_four, aes(x = year, y = gdp, color = country)) +
  geom_point(size = 2) +
  geom_line(linewidth = 0.8) +
  scale_color_brewer(palette = "Set1") +
  theme_minimal() +
  labs(
    title = "China's Rise to Become the Largest Economy",
    x = "Year",
    y = "GDP ($ trillion)",
    color = "Country"
  )

Calculating the total GDP by World Bank region

# Grouping the countries by region and year, then calculating their combined GDP
nations_regions <- nations %>%
  group_by(region, year) %>%
  summarise(GDP = sum(gdp, na.rm = TRUE))
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by region and year.
ℹ Output is grouped by region.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(region, year))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
# Previewing the regional GDP totals
head(nations_regions)
# A tibble: 6 × 3
# Groups:   region [1]
  region               year   GDP
  <chr>               <dbl> <dbl>
1 East Asia & Pacific  1990  5.52
2 East Asia & Pacific  1991  6.03
3 East Asia & Pacific  1992  6.50
4 East Asia & Pacific  1993  7.04
5 East Asia & Pacific  1994  7.64
6 East Asia & Pacific  1995  8.29
unique(nations_regions$region)
[1] "East Asia & Pacific"        "Europe & Central Asia"     
[3] "Latin America & Caribbean"  "Middle East & North Africa"
[5] "North America"              "South Asia"                
[7] "Sub-Saharan Africa"        

Comparing GDP across the World Bank regions

# Creating a stacked area chart to show total GDP by world region
ggplot(nations_regions, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", linewidth = 0.15) +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal() +
  labs(
    title = "GDP by World Bank Region",
    x = "Year",
    y = "GDP ($ trillion)",
    fill = "Region"
  )