Nations Assignment

Author

Paul D-O

## Load the “tidyverse” then read in the data

library(tidyverse)
library(plotly)
Warning: package 'plotly' was built under R version 4.4.3
setwd("C:/Users/Owner/Downloads")
nations <- read_csv("nations.csv")
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>

Use the mutate function to create a new column “gdp/capital * popilation/10^12”.

# Create a new column with a different name
nations1 <- nations |>
  mutate(total_gdp_trillions = (gdp_percap * population) / 10^12)
head(nations1)
# 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>, total_gdp_trillions <dbl>

For the first chart, filter the data with dplyr for the four desired countries ( South Africa,Egypt,Algeria and Nigeria ),top African coutries with the highest gdp in 2024 by Statista.

# Filter the dataset for specific African countries
filtered_african_nations <- filter(nations1, country %in% c("South Africa","Egypt, Arab Rep.", "Algeria", "Nigeria"))

Plot with geom_point and geom_line and plot GDP in trillions over time

plot0<- ggplot(filtered_african_nations, aes(x = year, y = total_gdp_trillions, color = country, group = country)) +
  geom_line() +
  geom_point() +
  scale_color_brewer(palette = "Set1") +
  labs(
    title = "GDP (Nigerian rise to becoming Africa largest economy",
    x = "Year",
    y = "GDP in Trillions"
  ) +
  theme_minimal()
plot0

While the above plot is interesting to view within the period 1990-2015,the reality is very different today.The above plot shows the impact of re-basing(capturing sectors of the economy that has never been captured in over 2 decades) the Nigeria economy. Today the throne belongs to South Africa.

loading the library plotly to create an interactive plot of plot 1

library(plotly)

# Create the ggplot chart
plot1 <- ggplot(filtered_african_nations, aes(x = year, y = total_gdp_trillions, color = country, group = country, 
                                              text = paste0("Total GDP (Trillions): ", total_gdp_trillions, 
                                                            "\nYear: ", year))) +
  geom_line() +
  geom_point() +
  scale_color_brewer(palette = "Set1") +
  labs(
    title = "GDP: Nigerian Rise to Becoming Africa's Largest Economy",
    x = "Year",
    y = "GDP in Trillions"
  ) +
  theme_minimal()

# Convert to an interactive plot with tooltips
interactive_filtered_african_nations <- ggplotly(plot1, tooltip = "text")

# Display the interactive plot
interactive_filtered_african_nations

For the second chart, using dplyr to group_by region and year, and then summarize on mutated value for gdp.

# Group by region and year, then summarize GDP
region_summary <- nations1 |>
  group_by(region, year) |>
  summarise(GDP = sum(total_gdp_trillions, na.rm = TRUE)) |>
ungroup()
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.

Create an area chart with a geom_area for region_summary

ggplot(region_summary, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", size = 0.6) +  # Add white outline
  scale_fill_brewer(palette = "Set2") +    # Use the Set2 palette
  labs(
    title = "GDP by World Bank Region",
    x = "Year",
    y = "GDP($Trillion)",
    fill = "Region"
  ) +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.