library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
nations<-as_tibble(read.csv("https://github.com/sdutky/mcData110/raw/master/corLmGgPlotly/nations.csv"))
eastAsiaPacific<- nations %>%
mutate(gdp=gdp_percap*population/1e+09) %>%
filter(region=="East Asia & Pacific") %>%
select(country,year,gdp) %>%
# Let's ignore these three boring countries:
filter(country != "China", country != "Australia", country!="Japan")
summary(eastAsiaPacific)
## country year gdp
## American Samoa : 25 Min. :1990 Min. : 0.0138
## Brunei Darussalam: 25 1st Qu.:1996 1st Qu.: 0.4274
## Cambodia : 25 Median :2002 Median : 11.4315
## Fiji : 25 Mean :2002 Mean : 178.9925
## French Polynesia : 25 3rd Qu.:2008 3rd Qu.: 176.7767
## Guam : 25 Max. :2014 Max. :2685.3089
## (Other) :650 NA's :164
candidates <- eastAsiaPacific %>%
group_by(country) %>%
summarise(meanGdp=mean(gdp)) %>%
arrange(desc(meanGdp)) %>%
head(n=4) %>%
select(country)
candidates
## # A tibble: 4 x 1
## country
## <fct>
## 1 Indonesia
## 2 Korea, Rep.
## 3 Thailand
## 4 Malaysia
theseFourTopGdp <- eastAsiaPacific %>%
filter(country %in% candidates$country) %>%
arrange(country,year)
# convert country from factor to character
theseFourTopGdp$country<-as.character(theseFourTopGdp$country)
summary(theseFourTopGdp)
## country year gdp
## Length:100 Min. :1990 Min. : 123.0
## Class :character 1st Qu.:1996 1st Qu.: 428.0
## Mode :character Median :2002 Median : 695.8
## Mean :2002 Mean : 829.5
## 3rd Qu.:2008 3rd Qu.:1047.7
## Max. :2014 Max. :2685.3
gGdp <- ggplot(data=theseFourTopGdp) +
aes(x=year,y=gdp,color=country)+
geom_line()+
geom_point()+
ylab("gdp (billions $US)")+
# center title over plot
theme(plot.title = element_text(hjust = 0.5))+
scale_color_brewer(palette = "Set1")+
# use leading new line to put title below ggplotly controls
ggtitle("\nAnnual GDP of These Four East Asian Countries")
# ggplotly(gGdp) # save for next week
gGdp
## Summarise each region by the sum of the per capita GDP of each of its countries
regions<- nations %>%
group_by(region, year) %>%
summarise(regionalPerCapGdp=sum(gdp_percap*population,na.rm = TRUE)/sum(population, na.rm = TRUE),sum=sum(gdp_percap,na.rm = TRUE))
regions$region<-as.character(regions$region)
regions$regionalPerCapGdp<-as.integer(regions$regionalPerCapGdp)
head(regions)
## # A tibble: 6 x 4
## # Groups: region [1]
## region year regionalPerCapGdp sum
## <chr> <int> <int> <dbl>
## 1 East Asia & Pacific 1990 3100 213116.
## 2 East Asia & Pacific 1991 3339 234287.
## 3 East Asia & Pacific 1992 3553 246209.
## 4 East Asia & Pacific 1993 3801 257732.
## 5 East Asia & Pacific 1994 4073 272159.
## 6 East Asia & Pacific 1995 4369 286105.
gGdp <- ggplot(data=regions) +
aes(x=year,y=sum,fill=region)+
#geom_line()+
#geom_point()+
geom_area( position = 'stack', colour="white", show.legend =TRUE)+
ylab("Summation per capita gdp ($US)")+
# center title over plot
theme(plot.title = element_text(hjust = 0.5))+
scale_color_brewer(palette = "Set2")+
# use leading new line to put title below ggplotly controls
ggtitle("\nAnnual Summation Per Capita GDP of Countries in Region")
#ggplotly(gGdp) # save for next week
gGdp
gGdp <- ggplot(data=regions) +
aes(x=year,y=regionalPerCapGdp,fill=region)+
#geom_line()+
#geom_point()+
geom_area( position = 'stack', colour="white", show.legend =TRUE)+
ylab("per capita gdp ($US)")+
# center title over plot
theme(plot.title = element_text(hjust = 0.5))+
scale_color_brewer(palette = "Set2")+
# use leading new line to put title below ggplotly controls
ggtitle("\nRegional Annual Per Capita GDP")
#ggplotly(gGdp) # save for next week
gGdp