Draw two charts using dplyr and ggplot2
library(readr)
library(ggplot2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.1
##
## 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
nations <- read_csv('nations.csv')
## Parsed with 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()
## )
dim(nations)
## [1] 5275 10
str(nations)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 5275 obs. of 10 variables:
## $ iso2c : chr "AD" "AD" "AD" "AD" ...
## $ iso3c : chr "AND" "AND" "AND" "AND" ...
## $ country : chr "Andorra" "Andorra" "Andorra" "Andorra" ...
## $ year : num 1996 1994 2003 1990 2009 ...
## $ gdp_percap : num NA NA NA NA NA NA NA NA NA NA ...
## $ population : num 64291 62707 74783 54511 85474 ...
## $ birth_rate : num 10.9 10.9 10.3 11.9 9.9 NA 10.9 9.8 11.8 11.2 ...
## $ neonat_mortal_rate: num 2.8 3.2 2 4.3 1.7 1.6 2 1.7 2.1 2.1 ...
## $ region : chr "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" ...
## $ income : chr "High income" "High income" "High income" "High income" ...
## - attr(*, "spec")=
## .. 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 using mutate
nations1<-nations%>% mutate(gdp= gdp_percap*population/1000000000000)%>%
filter(country=="China"|country=="Germany"|country=="Japan"|country=="United States")
ggplot(nations1, aes(x = year, y = gdp, color = country)) +
ylab("GDP($ trillion)") +
theme_minimal(base_size = 12) +
ggtitle("China's Rise to Become the Largest Economy") +
geom_point() +
geom_line() +
scale_color_brewer(palette = 'Set1')

nations2 <- nations %>% mutate(gdp= gdp_percap*population/1000000000000)%>%
group_by(region, year) %>% summarise(gdp= sum(gdp, na.rm = TRUE))
ggplot(nations2, aes(year, gdp, fill= region)) +
xlab("year") + ylab("GDP($ trillion)") +
theme_minimal(base_size = 12) +
scale_fill_brewer(palette = 'Set2') +
ggtitle("GDP by Word Bank Region") +
geom_area(colour = "white")
