Nations Dataset Chart Assignment

Author

A Porambo

Nations Dataset Chart Assignment

Load 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.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(ggplot2)
library(dplyr)
library(highcharter)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Highcharts (www.highcharts.com) is a Highsoft software product which is
not free for commercial and Governmental use
library(RColorBrewer)

Load Dataset.

nations <- read_csv('nations.csv') %>%
  mutate(gdp_tn = gdp_percap*population/1000000000000)
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.

Chart 1 - Dotted Line Chart

Filter your data down to only four nations.

four_nations <- nations %>% # Creates new "four_nations" dataset from "nations" dataset.
filter(iso3c == "CHN" | iso3c == "RUS" | iso3c == "FRA" | iso3c == "USA") %>% # Filter data down to data for China, Russia, France and the United States.
arrange(year) # Arrange data by year.

Plot multi-faceted dotted line chart.

four_nations_chart <- ggplot(four_nations, aes(x = year, y = gdp_tn, color = country)) + # Plots year agaist gdp in trillions from the four_nations dataset. Demarcates each country's separate line by color.
  labs(title = "Annual National Gross Domestic Product Over Time", x = "Year", y = "Annual National GDP (in trillions)") + # Adds labels for axes and a title. 
  geom_line(aes(group = country)) + # Creates lines for each country's data.
  geom_point() + # Adds points to mark the gdp values per year.
    scale_color_brewer(palette = "Set1") # Applies Set1 Color Brewer palette.
four_nations_chart

Chart 2 - Area Chart

Group data by regions, then year. Remove “n/a” values.

world_regions <- nations %>% # Creates new "world_regions" dataset from "nations" dataset.
group_by(region, year) %>% # Groups data first by region, then by year.
  summarise(GDP = sum(gdp_tn, na.rm = TRUE)) # Creates a new variable, "GDP" which is made from the sum of the gdps of all nations in a region per year. Removes "n/a" values.
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.

Plot segmented area chart.

ggplot(world_regions, aes(x = year, y = GDP, fill = region)) + # Plots year against GDP from the world_regions dataset. Demarcataes each region's area segment with color.
labs(title = "Annual Regional Gross Domestic Product Over Time", x = "Year", y = "Annual Regional GDP (in trillions)") + # Adds labels for axes and a title.
  geom_area(color = "white") + # Outlines each area in white.
  scale_fill_brewer(palette = "Set2") # Applies Set2 Color Brewer palette.