Loading up the nations dataset for the work ahead
library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(RColorBrewer)
nations <- read_csv("nations.csv")
## Rows: 5275 Columns: 10
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (5): iso2c, iso3c, country, region, income
## dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Creating a new variable in the data and Filtering the data for the four desired countries
nations_new <- nations %>%
select(country, year, gdp_percap, population, region) %>%
mutate(gdp_p1 = ((gdp_percap * population) / 1e+12) ) %>%
arrange(year)
str(nations_new)
## tibble [5,275 x 6] (S3: tbl_df/tbl/data.frame)
## $ country : chr [1:5275] "Andorra" "United Arab Emirates" "Afghanistan" "Antigua and Barbuda" ...
## $ year : num [1:5275] 1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ...
## $ gdp_percap: num [1:5275] NA 74017 NA 11087 2749 ...
## $ population: num [1:5275] 54511 1811458 12067570 61906 3286542 ...
## $ region : chr [1:5275] "Europe & Central Asia" "Middle East & North Africa" "South Asia" "Latin America & Caribbean" ...
## $ gdp_p1 : num [1:5275] NA 0.134079 NA 0.000686 0.009033 ...
nations_new_c1 <- nations_new %>%
filter(country %in% c("China", "Germany", "Japan", "United States")) %>%
ggplot(aes(x = year, y = gdp_p1))+
labs(title = "China's Rise to Become the Largest Economy") +
xlab("Year") +
ylab("GDP ($ trillion)") +
theme_minimal(base_size = 13) #changing the font size to 13 and theme to theme_minimal
nations_new_c1 +
geom_line(aes( color = country)) +
geom_point(aes(color = country)) +
labs(color = "Country") +
scale_color_brewer(palette = "Set1")

Creating the second chart
Grouping by region and year
by_regionyear <- nations %>%
mutate(gdp_p2= ((gdp_percap * population) / 1e+12) )
Summarizing the mutated value for gdp
regionyear <- by_regionyear %>%
group_by(region, year) %>%
summarise(gdp_p2 = sum(gdp_percap, na.rm = TRUE)) %>%
arrange(region , year)
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
p2 <- ggplot() +
geom_area(data = regionyear,aes(x = year, y = gdp_p2, fill = region), color = "white") +
scale_fill_brewer(name = "Region", palette = "Set2") +
ggtitle("GDP by World Bank Region" ) +
labs(
x = "Year",
y = "GDP (in trillion $)") +
theme_minimal(base_size = 13) #changing the font size to 13 and theme to theme_minimal
p2
