Nations Charts Assignments

Author

M Madinko

Nations Charts Assignments

Load the Library and Upload the Dataset

library(tidyverse)
library(plotly)
setwd("C:/Users/monik/OneDrive/Desktop/DATA 110")
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>

New Variable Created Using Mutate and remove the Na

nations2 <- nations|>
  mutate(country_gdp= (gdp_percap * population )/ 1000000000000)|>
  filter(!is.na(country_gdp))
nations2
# A tibble: 4,509 × 11
   iso2c iso3c country  year gdp_percap population birth_rate neonat_mortal_rate
   <chr> <chr> <chr>   <dbl>      <dbl>      <dbl>      <dbl>              <dbl>
 1 AE    ARE   United…  1991     73037.    1913190       24.6                7.9
 2 AE    ARE   United…  1993     71960.    2127863       22.4                7.3
 3 AE    ARE   United…  2001     83534.    3217865       15.8                5.5
 4 AE    ARE   United…  1992     73154.    2019014       23.5                7.6
 5 AE    ARE   United…  1994     74684.    2238281       21.3                6.9
 6 AE    ARE   United…  2007     75427.    6010100       12.8                4.7
 7 AE    ARE   United…  2004     87844.    3975945       14.2                5.1
 8 AE    ARE   United…  1996     79480.    2467726       19.3                6.4
 9 AE    ARE   United…  2006     82754.    5171255       13.3                4.9
10 AE    ARE   United…  2000     84975.    3050128       16.4                5.6
# ℹ 4,499 more rows
# ℹ 3 more variables: region <chr>, income <chr>, country_gdp <dbl>

Selecting four countries

# Keep only the four countries
my_countries <- nations2 %>%
  filter(country %in% c("Egypt, Arab Rep.", "Algeria", "South Africa", "Nigeria"))
my_countries
# A tibble: 100 × 11
   iso2c iso3c country  year gdp_percap population birth_rate neonat_mortal_rate
   <chr> <chr> <chr>   <dbl>      <dbl>      <dbl>      <dbl>              <dbl>
 1 DZ    DZA   Algeria  1994      6519.   28362015       26.4               21.7
 2 DZ    DZA   Algeria  2006     11341.   33749328       21.7               18.8
 3 DZ    DZA   Algeria  2014     14259.   38934334       24.3               15.6
 4 DZ    DZA   Algeria  2000      8094.   31183658       19.6               21  
 5 DZ    DZA   Algeria  1993      6575.   27785977       27.9               21.8
 6 DZ    DZA   Algeria  1992      6706.   27180921       29.4               22  
 7 DZ    DZA   Algeria  2001      8417.   31590320       19.3               20.9
 8 DZ    DZA   Algeria  2003      9622.   32394886       19.6               20.4
 9 DZ    DZA   Algeria  2010     12637.   36036159       24.6               16.5
10 DZ    DZA   Algeria  1991      6593.   26554277       30.8               22.1
# ℹ 90 more rows
# ℹ 3 more variables: region <chr>, income <chr>, country_gdp <dbl>

Line and point Plot

first_plot<- my_countries|>
ggplot(aes(x= year, y=country_gdp, color= country ))+
  geom_line()+
  geom_point()+
scale_color_brewer(palette = "Set1") +
labs(title = "Nigeria: The Largest Economy in Africa",
       x = "Year",
       y = "GDP (Trillions USD)",
  caption = "Source: Nations dataset"
  )
first_plot

Line and point Plot with interactivity using plotly

first_plot<- my_countries|>
ggplot(aes(x= year, y=country_gdp, color= country ))+
  geom_line()+
  geom_point()+
scale_color_brewer(palette = "Set1") +
labs(title = "Nigeria: The Largest Economy in Africa",
       x = "Year",
       y = "GDP (Trillions USD)",
  caption = "Source: Nations dataset"
  )
ggplotly(first_plot)

Summarize and Group by for the Second Plot

region_gdp <- nations2 |>
  group_by(region, year) |>
  summarise(GDP = sum(country_gdp, na.rm = TRUE))
region_gdp
# A tibble: 175 × 3
# Groups:   region [7]
   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
 7 East Asia & Pacific  1996  8.96
 8 East Asia & Pacific  1997  9.55
 9 East Asia & Pacific  1998  9.60
10 East Asia & Pacific  1999 10.1 
# ℹ 165 more rows

The Area Plot

second_plot<- region_gdp |>
ggplot(aes(x= year, y=GDP, fill= region ))+
  geom_area( color = "white")+
scale_color_brewer(palette = "Set2") +
labs(title = "GDP by Region",
       x = "Year",
       y = "Total GDP ($Trillions)",
  caption = "Source: Nations dataset"
  ) +
theme_minimal(base_size = 12)
second_plot

Area Plot with interactivity using plotly

second_plot<- region_gdp |>
ggplot(aes(x= year, y=GDP, fill= region ))+
  geom_area( color = "white")+
scale_color_brewer(palette = "Set2") +
labs(title = "GDP by Region",
       x = "Year",
       y = "Total GDP ($Trillions)",
caption = "Source: Nations dataset") +
  theme_minimal(base_size = 12)
ggplotly(second_plot)

Load the Libraries and Alluvial Plot

library(alluvial)
library(ggalluvial)
allulvial_ggalluv <- region_gdp |>
  ggplot(aes(x = year, y = GDP, alluvium = region)) + 
  theme_bw() +
  geom_alluvium(aes(fill = region), 
                color = "white",
                size = 0.2,
                width = 0.1, 
                alpha = 0.9,
                decreasing = FALSE) +
  scale_fill_brewer(palette = "Spectral") + 
  labs(title = "GDP by Region",
       y = "Total GDP ($Trillions)",
  caption = "Source: Nations dataset")+
  theme_minimal(base_size = 12)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
fill = "region"
allulvial_ggalluv

Alluvial Plot and Interactivity

library(alluvial)
library(ggalluvial)
allulvial_ggalluv <- region_gdp |>
  ggplot(aes(x = year, y = GDP, alluvium = region)) + 
  theme_bw() +
  geom_alluvium(aes(fill = region), 
                color = "white",
                size = 0.2,
                width = 0.1, 
                alpha = 0.9,
                decreasing = FALSE) +
  scale_fill_brewer(palette = "Spectral") + 
  labs(title = "GDP by Region",
       y = "Total GDP ($Trillions)",
  caption = "Source: Nations dataset")+
  theme_minimal(base_size = 12)
fill = "region"
ggplotly(allulvial_ggalluv)