Nations Charts Assignments

Author

Ayomide Joe-Adigwe

load the required libraries

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(RColorBrewer)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ lubridate 1.9.3     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.1
✔ readr     2.1.5     
── 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

Setting Working Directory and Loading Data with dplyr

library(dplyr)
setwd("/Users/ayomidealagbada/AYOMIDE'S DATAVISUALITIOM")
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.

Adding a new variable for GDP in trillions

nations <- nations %>%
  mutate(gdp_trillions = (gdp_percap * population) / 10^12)

Filter for the four countries

selected_countries <- nations %>%
  filter(country %in% c("China", "Germany", "Japan", "United States"))

Plotting the first chart with both geom_point and geom_line

ggplot(selected_countries, aes(x = year, y = gdp_trillions, color = country)) +
  geom_point() +                                # Add points
  geom_line() +                                 # Add lines connecting points
  scale_color_brewer(palette = "Set1") +        # Use the Set1 color palette from ColorBrewer
  labs(title = "China's Rise to Become the Largest Economy", 
       x = "Year", 
       y = "GDP (Trillions of $)",
       color = "Country") +
  theme_minimal()

head(selected_countries)
# 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 CN    CHN   China    1992      1260. 1164970000       18.3               29.4
2 CN    CHN   China    2005      5053. 1303720000       12.4               14  
3 CN    CHN   China    2000      2915. 1262645000       14.0               21.2
4 CN    CHN   China    1991      1091. 1150780000       19.7               29.7
5 CN    CHN   China    2013     12219. 1357380000       12.1                6.3
6 CN    CHN   China    1999      2650. 1252735000       14.6               22.2
# ℹ 3 more variables: region <chr>, income <chr>, gdp_trillions <dbl>
class(selected_countries$gdp_trillions)
[1] "numeric"

Step 1: Group by region and year, then summarize the total GDP per region

gdp_by_region <- nations %>%
  group_by(region, year) %>%
   summarise(total_gdp_trillions = sum((gdp_percap * population) / 10^12, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.

Step 2: Plot the area chart using ggplot2

ggplot(gdp_by_region, aes(x = year, y = total_gdp_trillions, fill = region)) +
  geom_area() +                                 # Use geom_area for stacked areas
  scale_fill_brewer(palette = "Set2") +         # Use the Set2 color palette
  labs(title = "GDP by World Bank Region", 
       x = "Year", 
       y = "GDP (Trillions of $)",
       fill = "Region") +
  theme_minimal()