Nations Charts

Author

Ash Ibasan

Introduction

Let’s take a look at two charts created from the Nations dataset, where we’ll see how GDP has changed over time for four selected countries and how regions compare in terms of GDP across different years.

Load tidyverse and plotly libraries

# Sat Oct 12 19:54:41 2024 ------------------------------
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.4.1
Warning: package 'ggplot2' was built under R version 4.4.1
Warning: package 'tibble' was built under R version 4.4.1
Warning: package 'tidyr' was built under R version 4.4.1
Warning: package 'readr' was built under R version 4.4.1
Warning: package 'purrr' was built under R version 4.4.1
Warning: package 'dplyr' was built under R version 4.4.1
Warning: package 'stringr' was built under R version 4.4.1
Warning: package 'forcats' was built under R version 4.4.1
Warning: package 'lubridate' was built under R version 4.4.1
── 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
library(plotly) #for interactivity
Warning: package 'plotly' was built under R version 4.4.1

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout

Load Nations dataset

nations <- 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.

The echo: false option disables the printing of code (only output is displayed).

Data Preparation

nations <- nations %>%
  mutate(gdp = (gdp_percap * population) / 1e12)

First Chart - GDP over time for four countries

four_countries <- c("Egypt, Arab Rep.", "Philippines", "Turkey", "Finland")
nations_filtered <- nations %>%
  filter(country %in% four_countries)

Scatterplot ONE

ggplot(nations_filtered, aes(x = year, y = gdp, color = country)) +
  geom_line() + 
  geom_point() + 
  scale_color_brewer(palette = "Set1") +
  labs(title = "Economic Growth Over Time: A Four-Country Comparison",
       x = "Year",
       y = "GDP, Trillions (US$)", 
       caption = "Source: World Bank Data") + 
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Interactive Scatterplot

library(plotly)
scatter_gg <- ggplot(nations_filtered, aes(x = year, y = gdp, color = country)) +
  geom_line() + 
  geom_point() + 
  scale_color_brewer(palette = "Set1") +
  labs(title = "Economic Growth Over Time: A Four-Country Comparison",
       x = "Year",
       y = "GDP, Trillions (US$)", 
       caption = "Source: World Bank Data") + 
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplotly(scatter_gg) 

Second Chart - Regional GDP Over Time

summarized_nations <- nations %>%
  group_by(region, year) %>%
  summarize(GDP = sum(gdp, na.rm = TRUE), .groups = 'drop')
head(summarized_nations)
# A tibble: 6 × 3
  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

Area Plot TWO

ggplot(summarized_nations, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", linewidth = 0.5, alpha = 0.7) +
  scale_fill_brewer(palette = "Set2") + 
  labs(title = "GDP Trends Across Regions",
       x = "Year",
       y = "GDP, Trillions (US$)",
       caption = "Source: World Bank Data") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Interactive Stacked Area Plot

plot_ly(data = summarized_nations, 
        x = ~year, 
        y = ~GDP, 
        color = ~region, 
        type = 'scatter', 
        mode = 'none', 
        fill = 'tozeroy',
        hoverinfo = 'x+y+text',
        text = ~region) %>%
  layout(title = "GDP Trends Across Regions",
         xaxis = list(title = "Year"),
         yaxis = list(title = "GDP, Trillions (US$)"),
         showlegend = TRUE)