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>
# 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>
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)