Nations Highchart Activity

Nations Charts Activity

#load the libraries 
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
library(dplyr) 
library(ggplot2) 
library(RColorBrewer)
# load data
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.

Represent GDP in trillions

nations <- read_csv("nations.csv") |>
  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.

Geom_point and Geom_line

#filter data
big4 <- nations |>
  filter(iso3c %in% c("CHN","DEU", "JPN", "USA")) |>
  arrange(year)

#Make basic chart
nations_chart <- ggplot(big4, aes(x = year, y = gdp_tn, group = country)) +
  labs(title = "China's Rise to Become the Largest Economy") +
  xlab("Year") +
  ylab("GDP ($ trillion)") + 
  theme_minimal(base_size = 14)
#Add layers
nations_chart +
  geom_line(aes(color = country)) +
  geom_point(aes(color = country)) +
  scale_color_brewer(palette = "Set1")

Area Chart

# prepare data
regions <- nations |>
  group_by(year,region) |>
  summarize(gdp_tn = sum(gdp_tn, na.rm = TRUE)) |>
  arrange(year,region) 
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by year and region.
ℹ Output is grouped by year.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(year, region))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
#plot area chart
area <- regions |>
  ggplot(aes(x = year, y = gdp_tn)) +
  theme_bw() +
  labs(title = "GDP by World Bank Region",
       y = "GDP ($ trillion)",
       x = "year") +
  geom_area(aes(fill = region), color = "white") +
  scale_color_brewer(palette = "Set2") +
  labs(fill = "region")

area