Nations Dataset Charts Assignment

Author

Daniel B HW

Load Libraries and Dataset

Load libraries

library(tidyverse)
library(ggplot2)
library(dplyr)

Set working directory and load dataset

setwd("C:/Users/Administrator/Documents/Data110/Datasets")
nations <- read_csv("nations.csv")

Clean headers and view data

names(nations) <- tolower(names(nations))
names(nations) <- gsub(" ","",names(nations))
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>

Show countries to select from

unique_countries <- sort(unique(nations$country))
print(unique_countries)
  [1] "Afghanistan"                    "Albania"                       
  [3] "Algeria"                        "American Samoa"                
  [5] "Andorra"                        "Angola"                        
  [7] "Antigua and Barbuda"            "Argentina"                     
  [9] "Armenia"                        "Aruba"                         
 [11] "Australia"                      "Austria"                       
 [13] "Azerbaijan"                     "Bahamas, The"                  
 [15] "Bahrain"                        "Bangladesh"                    
 [17] "Barbados"                       "Belarus"                       
 [19] "Belgium"                        "Belize"                        
 [21] "Benin"                          "Bermuda"                       
 [23] "Bhutan"                         "Bolivia"                       
 [25] "Bosnia and Herzegovina"         "Botswana"                      
 [27] "Brazil"                         "Brunei Darussalam"             
 [29] "Bulgaria"                       "Burkina Faso"                  
 [31] "Burundi"                        "Cambodia"                      
 [33] "Cameroon"                       "Canada"                        
 [35] "Cayman Islands"                 "Central African Republic"      
 [37] "Chad"                           "Channel Islands"               
 [39] "Chile"                          "China"                         
 [41] "Colombia"                       "Comoros"                       
 [43] "Congo, Dem. Rep."               "Congo, Rep."                   
 [45] "Costa Rica"                     "Cote d'Ivoire"                 
 [47] "Croatia"                        "Cuba"                          
 [49] "Curacao"                        "Cyprus"                        
 [51] "Czech Republic"                 "Denmark"                       
 [53] "Djibouti"                       "Dominica"                      
 [55] "Dominican Republic"             "Ecuador"                       
 [57] "Egypt, Arab Rep."               "El Salvador"                   
 [59] "Equatorial Guinea"              "Eritrea"                       
 [61] "Estonia"                        "Ethiopia"                      
 [63] "Fiji"                           "Finland"                       
 [65] "France"                         "French Polynesia"              
 [67] "Gabon"                          "Gambia, The"                   
 [69] "Georgia"                        "Germany"                       
 [71] "Ghana"                          "Gibraltar"                     
 [73] "Greece"                         "Greenland"                     
 [75] "Grenada"                        "Guam"                          
 [77] "Guatemala"                      "Guinea"                        
 [79] "Guinea-Bissau"                  "Guyana"                        
 [81] "Haiti"                          "Honduras"                      
 [83] "Hong Kong SAR, China"           "Hungary"                       
 [85] "Iceland"                        "India"                         
 [87] "Indonesia"                      "Iran, Islamic Rep."            
 [89] "Iraq"                           "Ireland"                       
 [91] "Isle of Man"                    "Israel"                        
 [93] "Italy"                          "Jamaica"                       
 [95] "Japan"                          "Jordan"                        
 [97] "Kazakhstan"                     "Kenya"                         
 [99] "Kiribati"                       "Korea, Rep."                   
[101] "Kuwait"                         "Kyrgyz Republic"               
[103] "Lao PDR"                        "Latvia"                        
[105] "Lebanon"                        "Lesotho"                       
[107] "Liberia"                        "Libya"                         
[109] "Liechtenstein"                  "Lithuania"                     
[111] "Luxembourg"                     "Macao SAR, China"              
[113] "Macedonia, FYR"                 "Madagascar"                    
[115] "Malawi"                         "Malaysia"                      
[117] "Maldives"                       "Mali"                          
[119] "Malta"                          "Marshall Islands"              
[121] "Mauritania"                     "Mauritius"                     
[123] "Mexico"                         "Micronesia, Fed. Sts."         
[125] "Moldova"                        "Monaco"                        
[127] "Mongolia"                       "Montenegro"                    
[129] "Morocco"                        "Mozambique"                    
[131] "Myanmar"                        "Namibia"                       
[133] "Nepal"                          "Netherlands"                   
[135] "New Caledonia"                  "New Zealand"                   
[137] "Nicaragua"                      "Niger"                         
[139] "Nigeria"                        "Northern Mariana Islands"      
[141] "Norway"                         "Oman"                          
[143] "Pakistan"                       "Palau"                         
[145] "Panama"                         "Papua New Guinea"              
[147] "Paraguay"                       "Peru"                          
[149] "Philippines"                    "Poland"                        
[151] "Portugal"                       "Puerto Rico"                   
[153] "Qatar"                          "Romania"                       
[155] "Russian Federation"             "Rwanda"                        
[157] "Samoa"                          "San Marino"                    
[159] "Sao Tome and Principe"          "Saudi Arabia"                  
[161] "Senegal"                        "Serbia"                        
[163] "Seychelles"                     "Sierra Leone"                  
[165] "Singapore"                      "Sint Maarten (Dutch part)"     
[167] "Slovak Republic"                "Slovenia"                      
[169] "Solomon Islands"                "Somalia"                       
[171] "South Africa"                   "South Sudan"                   
[173] "Spain"                          "Sri Lanka"                     
[175] "St. Kitts and Nevis"            "St. Lucia"                     
[177] "St. Martin (French part)"       "St. Vincent and the Grenadines"
[179] "Sudan"                          "Suriname"                      
[181] "Swaziland"                      "Sweden"                        
[183] "Switzerland"                    "Syrian Arab Republic"          
[185] "Tajikistan"                     "Tanzania"                      
[187] "Thailand"                       "Timor-Leste"                   
[189] "Togo"                           "Tonga"                         
[191] "Trinidad and Tobago"            "Tunisia"                       
[193] "Turkey"                         "Turkmenistan"                  
[195] "Turks and Caicos Islands"       "Tuvalu"                        
[197] "Uganda"                         "Ukraine"                       
[199] "United Arab Emirates"           "United Kingdom"                
[201] "United States"                  "Uruguay"                       
[203] "Uzbekistan"                     "Vanuatu"                       
[205] "Venezuela, RB"                  "Vietnam"                       
[207] "Virgin Islands (U.S.)"          "West Bank and Gaza"            
[209] "Yemen, Rep."                    "Zambia"                        
[211] "Zimbabwe"                      

First Chart

# Filter 4 Countries
countries_to_filter <- c("United States", "Russian Federation", "China", "Germany")

# Create a new variable for GDP in trillions
chart1 <- nations |>
  filter(country %in% countries_to_filter) |>
  mutate(gdp_trillions = (gdp_percap * population) / 1e12)

# Create a line graph
ggplot(chart1, aes(x = year, y = gdp_trillions, color = country)) +
  geom_line() +
  geom_point() + 
  scale_color_brewer(palette = "Set1") +
  labs(x = "year", y = "GDP ($ trillion)", color = NULL,title = "China's Rise to Become the Largest Economy")

Second Chart

# Create variable, group, and summarize
chart2 <- nations |>
  mutate(gdp_trillions = (gdp_percap * population) / 1e12) |>
  group_by(region, year) |>
  summarise(GDP = sum(gdp_trillions, na.rm = TRUE))

# Create chart2
ggplot(chart2, aes(x = year, y = GDP, fill = region)) +
  geom_area() +  
  scale_fill_brewer(palette = "Set2") +  
  labs(x = "year", y = "GDP ($ trillion)", fill = "region",title = "GDP by World Bank Region")