Nations Visualization

Author

Dany Drammeh

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── 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
setwd("C:/Users/danyd/OneDrive/Desktop/data 110/week6hw")
nations <- read.csv("nations.csv")

View the information in the dataset

head(nations)
  iso2c iso3c country year gdp_percap population birth_rate neonat_mortal_rate
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.0
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
                 region      income
1 Europe & Central Asia High income
2 Europe & Central Asia High income
3 Europe & Central Asia High income
4 Europe & Central Asia High income
5 Europe & Central Asia High income
6 Europe & Central Asia High income
tail(nations)
     iso2c iso3c  country year gdp_percap population birth_rate
5270    ZW   ZWE Zimbabwe 2009   1229.382   13720997     36.094
5271    ZW   ZWE Zimbabwe 2010   1360.876   13973897     36.267
5272    ZW   ZWE Zimbabwe 2011   1523.622   14255592     36.264
5273    ZW   ZWE Zimbabwe 2012   1679.126   14565482     36.077
5274    ZW   ZWE Zimbabwe 2013   1743.211   14898092     35.715
5275    ZW   ZWE Zimbabwe 2014   1798.060   15245855     35.189
     neonat_mortal_rate             region     income
5270               25.2 Sub-Saharan Africa Low income
5271               25.3 Sub-Saharan Africa Low income
5272               25.1 Sub-Saharan Africa Low income
5273               24.8 Sub-Saharan Africa Low income
5274               24.4 Sub-Saharan Africa Low income
5275               23.9 Sub-Saharan Africa Low income
summary(nations)
    iso2c              iso3c             country               year     
 Length:5275        Length:5275        Length:5275        Min.   :1990  
 Class :character   Class :character   Class :character   1st Qu.:1996  
 Mode  :character   Mode  :character   Mode  :character   Median :2002  
                                                          Mean   :2002  
                                                          3rd Qu.:2008  
                                                          Max.   :2014  
                                                                        
   gdp_percap         population          birth_rate    neonat_mortal_rate
 Min.   :   239.7   Min.   :9.004e+03   Min.   : 6.90   Min.   : 0.70     
 1st Qu.:  2263.6   1st Qu.:7.175e+05   1st Qu.:13.40   1st Qu.: 6.70     
 Median :  6563.2   Median :5.303e+06   Median :21.60   Median :15.00     
 Mean   : 12788.8   Mean   :2.958e+07   Mean   :24.16   Mean   :19.40     
 3rd Qu.: 17195.0   3rd Qu.:1.757e+07   3rd Qu.:33.88   3rd Qu.:29.48     
 Max.   :141968.1   Max.   :1.364e+09   Max.   :55.12   Max.   :73.10     
 NA's   :766        NA's   :14          NA's   :295     NA's   :525       
    region             income         
 Length:5275        Length:5275       
 Class :character   Class :character  
 Mode  :character   Mode  :character  
                                      
                                      
                                      
                                      

Create new variables

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

GDP_in_billions <- nations %>%
  mutate(GDP_Billions = (gdp_percap * population)/ 10^9)

Plot 1

nationsfour <- GDP_in_billions %>%
  filter(country %in% c("Eritrea", "Gambia, The", "Ethiopia", "United Kingdom"))

ggplot(nationsfour, aes(x = year, y = GDP_Billions, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  labs(
    title = "GDP Trends of Four Countries",
    x = "Year",
    y = "GDP (Billions of Dollars)"
  )
Warning: Removed 5 rows containing missing values (`geom_point()`).
Warning: Removed 5 rows containing missing values (`geom_line()`).

Plot 2

nationcharttwo <- GDP_in_trillions %>%
  group_by(region, year) %>%
  summarize(GDP_Trillions = sum(GDP_Trillions, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
ggplot(nationcharttwo, aes(x = year, y = GDP_Trillions, fill = region)) +
  geom_area(color = "white", size = 0.2) +
  scale_fill_brewer(palette = "Set2") +
  labs(
    title = "GDP by World Bank Region",
    x = "Year",
    y = "GDP (in $ Trillions)"
  ) +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.