Nations HW

Author

Z Griffin

Nations Chart HW

Load the libraries and the dataset

library(tidyverse)
library(ggplot2)
setwd("~/Schol Stuff/Montgomery College 2025/Data 110 Data Visualization/Nations chart HW")
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>

Create variable gpd using mutate

nations2 <- nations |>
  mutate(gdp = gdp_percap * population/ 10^12)

head(nations2)
# 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>, gdp <dbl>

Chart One: GDP of Four Nations

Identify all the countries in the dataset.

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

Filter for my desired four countries: Canada, Egypt, Israel, and Italy. Note that Egypt’s full name is Egypt, Arab Rep. That didn’t cause any issues, but just ‘Egypt’ looks better on the graph, so I’m also renaming that.

nations3 <- nations2 |>
  filter(country %in% c('Canada', 'Egypt, Arab Rep.', 'Israel', 'Italy'))

nations3$country[nations3$country == 'Egypt, Arab Rep.'] <- 'Egypt' 

head(nations3)
# 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 CA    CAN   Canada   2000     29188.   30769700       10.9                3.7
2 CA    CAN   Canada   1990     20085.   27791000       15                  4.4
3 CA    CAN   Canada   1996     23888.   29671900       12                  4  
4 CA    CAN   Canada   2013     44281.   35155499       10.9                3.4
5 CA    CAN   Canada   2008     40278.   33245773       11.3                3.8
6 CA    CAN   Canada   2004     33752.   31995000       10.6                4  
# ℹ 3 more variables: region <chr>, income <chr>, gdp <dbl>

Make the plot

p1 <- nations3 |>
  ggplot(aes(x = year, y = gdp, color = country)) +
  theme_minimal() +
  labs(title = 'GDP from 1990 to 2014',
       x = 'Year',
       y = 'GDP ($ trillions)',
       color = '') +
  geom_line(linewidth = .9) +
  geom_point( size = 2.7) +
  scale_color_manual(values = c("#d42013", "#9b34eb", "#2730e3", "#e6ad45" ))

p1

Chart TWo: Area Chart GDP per region

Find total GDP per region

regions <- nations2 |>
  group_by(region, year) |>
  summarize(GDP = sum(gdp, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
head(regions)
# A tibble: 6 × 3
# Groups:   region [1]
  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

Build the plot

p2 <- ggplot(regions,aes(x = year, y = GDP, fill = region))+
  theme_minimal()+
  scale_fill_manual(values = c("#db0e07", "#ede72f","#f09516","#0ca616","#21e0ed","#4c1ff0", "#951ff0" )) +
  labs(title = 'GDP by World Bank Regions',
       x = 'Year',
       y = 'GDP ($ trillions)', 
       fill = 'Region') 
p2 + geom_area(color = 'white', linewidth = .7)