library(tidyverse)
library(RColorBrewer)
setwd("C:/Users/Marti/OneDrive/Desktop/MC-DV")
nations <- read_csv("nations.csv")Nations
Load Libraries and Set Working Directory
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>
Add new column for GDP in Trillions
nations1 <-nations |>
mutate(gdp_tril = gdp_percap*population/10^12)Filter out for only desired five countries
nations_US_allies <-nations1 |>
filter(country %in% c("United States", "Australia", "United Kingdom", "Japan"))
head(nations_US_allies)# 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 AU AUS Austral… 2007 36563. 20827600 14.1 3.1
2 AU AUS Austral… 2014 45937. 23464086 12.9 2.3
3 AU AUS Austral… 2008 37479. 21249200 14 3
4 AU AUS Austral… 2012 42596. 22728254 13.7 2.5
5 AU AUS Austral… 1997 22991. 18517000 13.6 3.6
6 AU AUS Austral… 1993 19165. 17667000 14.7 4
# ℹ 3 more variables: region <chr>, income <chr>, gdp_tril <dbl>
Plot Comparison of US & Allies GDP
ggplot(nations_US_allies,
aes(x = year, y = gdp_tril, color = country)) +
geom_point() +
geom_line()+
xlab("Year") +
ylab("GDP in Trillon") +
ggtitle("Comparison of US & Allies GDP") +
scale_fill_brewer(palette = "Set1") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))Group by Country and Year
grouped_nations <-nations1 |>
group_by(region,year) |>
summarise(
gdp_tril = sum(gdp_tril,na.rm = TRUE),
.groups = 'drop' # This will ungroup after summarisation
)Plot Area Plot
ggplot(grouped_nations,
aes(x = year, y = gdp_tril, fill = region)) +
geom_area(color ="white", size = 0.3)+
xlab("Year") +
ylab("GDP in Trillon") +
ggtitle("Comparison of US & Allies GDP") +
scale_fill_brewer(palette = "Set2") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.