# load required packages
library(readr) 
library(ggplot2) 
library(scales) 
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor
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
# install highcharter, RColorBrewer
# install.packages("highcharter","RColorBrewer") 
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
library(RColorBrewer)
setwd("~/Downloads")
nations<- read_csv("nations.csv") %>%
  mutate(gdp_tn = gdp_percap*population/1000000000000)
## 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
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# prepare data
big4 <- nations %>%
  filter(iso3c == "CHN" | iso3c == "DEU" | iso3c == "JPN" | iso3c == "USA") %>%
   arrange(year)
# basic symbol-and-line chart, default settings
highchart() %>%
  hc_add_series(data = big4,
                   type = "line", hcaes(x = year,
                   y = gdp_tn,
                   group = country))
# define color palette
cols <- brewer.pal(4, "Set1")
highchart() %>%
  hc_add_series(data = big4,
                   type = "line", hcaes(x = year,
                                        y = gdp_tn,
                                       group = country)) %>%
  hc_colors(cols)
highchart() %>%
  hc_add_series(data = big4,
                   type = "line",
                   hcaes(x = year,
                   y = gdp_tn,
                   group = country)) %>%
  hc_colors(cols) %>%
  hc_xAxis(title = list(text="Year")) %>%
 hc_yAxis(title = list(text="GDP ($ trillion)"))
highchart() %>%
  hc_add_series(data = big4,
                   type = "line",
                   hcaes(x = year,
                   y = gdp_tn,
                   group = country)) %>%
  hc_colors(cols) %>%
  hc_xAxis(title = list(text="Year")) %>%
  hc_yAxis(title = list(text="GDP ($ trillion)")) %>%
  hc_plotOptions(series = list(marker = list(symbol = "circle"))) %>%
  hc_legend(align = "right",
            verticalAlign = "top")
# customize the tooltips
big4_chart <- highchart() %>%
  hc_add_series(data = big4,
                   type = "line",
                   hcaes(x = year,
                   y = gdp_tn,
                   group = country)) %>%
  hc_colors(cols) %>%
  hc_xAxis(title = list(text="Year")) %>%
  hc_yAxis(title = list(text="GDP ($ trillion)")) %>%
  hc_plotOptions(series = list(marker = list(symbol = "circle"))) %>%
  hc_legend(align = "right",
            verticalAlign = "top") %>%
  hc_tooltip(shared = TRUE,
             borderColor = "black",
             pointFormat = "{point.country}: {point.gdp_tn:.2f}<br>")
big4_chart
# prepare data
regions <- nations %>%
  group_by(year,region) %>%
  summarize(gdp_tn = sum(gdp_tn, na.rm = TRUE)) %>%
  arrange(year,region)
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
# set color palette
cols <- brewer.pal(7, "Set2")
# stacked area chart
highchart () %>%
  hc_add_series(data = regions,
                   type = "area",
                   hcaes(x = year,
                   y = gdp_tn,
                   group = region)) %>%
  hc_colors(cols) %>%
  hc_chart(style = list(fontFamily = "Georgia",
                        fontWeight = "bold")) %>%
  hc_plotOptions(series = list(stacking = "normal",
                               marker = list(enabled = FALSE,
                               states = list(hover = list(enabled = FALSE))),
                               lineWidth = 0.5,
                               lineColor = "white")) %>%
  hc_xAxis(title = list(text="Year")) %>%
  hc_yAxis(title = list(text="GDP ($ trillion)")) %>%
  hc_legend(align = "right", verticalAlign = "top",
            layout = "vertical") %>%
  hc_tooltip(enabled = FALSE)