I recreated two graphs that display the growth of gross domestic product (GDP) in trillions of dollars. Gross domestic product is the total monetary or market value of all the finished goods and services produced within a country’s borders in a specific time period. My family’s country of origin is Peru. In the first graph, I decided to do a comparison of the GDP growth our country of origin to the three countries my family has immigrated to in search of better opportunities.The second graph shows the GDP growth of the regions covered by the World Bank.
Load required libraries:
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.0 v dplyr 1.0.4
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## Warning: package 'stringr' was built under R version 4.0.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(RColorBrewer)
Upload dataset:
setwd("C:/Users/mivul/OneDrive/Desktop/Data 110/Datasets")
nation<-read_csv("nations.csv")
##
## -- Column specification --------------------------------------------------------
## cols(
## iso2c = col_character(),
## iso3c = col_character(),
## country = col_character(),
## year = col_double(),
## gdp_percap = col_double(),
## population = col_double(),
## birth_rate = col_double(),
## neonat_mortal_rate = col_double(),
## region = col_character(),
## income = col_character()
## )
Create a new variable called gdp_in_trillions for GDP of each country in trillions of dollars:
nation2<- nation %>%
mutate(gdp_in_trillions = (gdp_percap * population / 1*10^12))
Filter for the four countries which will be observed:
nation4 <- nation2 %>%
filter(country == "Peru" | country == "United States" | country == "Spain" | country == "France")
Construct the graph to show the comparison of GDP growth of the four countries:
ggplot(nation4, aes(x=year, y= gdp_in_trillions))+
geom_point(aes(color = country))+
geom_line(aes(color = country))+
ggtitle("Economic Growth of Countries My Family Resides In")+
ylab("GDP ($ trillion)")+
#set color palette
scale_color_brewer(palette = "Set1") +
#change the labels of the y-axis
scale_y_continuous(labels = c("0","5","10","15","20"))+
#set theme to white background
theme_minimal()
Create a new table that summarizes the sum of the variable gdp_in_trillions by region and country:
nation3 <- nation2 %>%
group_by(region, year) %>%
summarise(GDP = sum(gdp_in_trillions, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the `.groups` argument.
Construct the graph to show the GDP growth of each region classified by the World Bank:
nation3 %>%
ggplot(aes(x = year,y = GDP, fill= region)) +
geom_area() +
ylab("GDP ($ trillion)")+
ggtitle("GDP by World Bank Region")+
#set color palette
scale_fill_brewer(palette = "Set2")+
#change label of y-axis from
scale_y_continuous(labels = c("0","25","50","75","100"))+
#set theme to white background
theme_minimal()