library(rvest)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.1.8
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()         masks stats::filter()
## ✖ readr::guess_encoding() masks rvest::guess_encoding()
## ✖ dplyr::lag()            masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(ggsci)
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
library(DT)
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)
library(ggplot2)

#Loading the data

setwd("C:/Users/maddi/OneDrive/Desktop/DATA110")
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
## 
## ℹ 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.
nations <- read_csv("nations.csv") %>% 
  mutate(gdp_tn = gdp_percap*population/1e+12 )
## 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.

#Happy Nations

happy5 <- nations %>% 
  filter(iso3c == "FIN" | iso3c == "DNK" | iso3c == "NOR" | iso3c == "ISL" | iso3c == "NLD") %>% 
  arrange(year)
happy5order <- nations %>% 
  filter(iso3c == "FIN" | iso3c == "DNK" | iso3c == "NOR" | iso3c == "ISL" | iso3c == "NLD") %>% 
  arrange(year)
happylegend <- factor(happy5, levels = c("Finland", "Denmark", "Norway", "Iceland", "Netherlands"))
highchart() %>%
  hc_add_series(data = happy5,
                   type = "line", hcaes(x = year,
                   y = gdp_tn, 
                   group = country))
highchart() %>%
  hc_add_series(data = happy5,
                   type = "column", hcaes(x = year,
                   y = gdp_tn, 
                   group = country))
highchart() %>%
  hc_add_series(data = happy5,
                   type = "scatter", color = "purple", 
                hcaes(x = year,
                   y = gdp_tn, 
                   group = country))
highchart() %>%
  hc_add_series(data = happy5,
                   type = "line", 
                hcaes(x = year,
                   y = gdp_tn, 
                   group = country)) %>% 
  hc_legend(labels("Finland", "Denmark", "Norway", "Iceland", "Netherlands"))
cols <- brewer.pal(5, "Pastel1")
highchart() %>%
  hc_add_series(data = happy5,
                   type = "line", hcaes(x = year,
                   y = gdp_tn, 
                   group = country)) %>%
  hc_colors(cols)
cols2 <- brewer.pal(5, "RdPu")
highchart() %>%
  hc_add_series(data = happy5,
                   type = "line", hcaes(x = year,
                   y = gdp_tn, 
                   group = country)) %>%
  hc_colors(cols2)
cols3 <- brewer.pal(5, "Set2")
highchart() %>%
  hc_add_series(data = happy5,
                   type = "line", hcaes(x = year,
                   y = gdp_tn, 
                   group = country)) %>%
  hc_colors(cols3)
highchart() %>%
  hc_add_series(data = happy5,
                   type = "line",
                   hcaes(x = year,
                   y = gdp_tn, 
                   group = country)) %>%
  hc_colors(cols3) %>%
  hc_xAxis(title = list(text="Year")) %>%
  hc_yAxis(title = list(text="GDP ($ trillion)"))
highchart() %>%
  hc_add_series(data = happy5order,
                   type = "line",
                   hcaes(x = year,
                   y = gdp_tn, 
                   group = country)) %>%
  hc_colors(cols3) %>%
  hc_title(text ="GDP Growth in World's 5 Happiest Countries") %>%
  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")

##Fastest growing economies

growing5 <- nations %>% 
  filter(iso3c == "CHN" | iso3c == "IND" | iso3c == "IDN" | iso3c == "KEN" | iso3c == "PHL") %>% 
  arrange(year)
highchart() %>%
  hc_add_series(data = growing5,
                   type = "line",
                   hcaes(x = year,
                   y = gdp_tn, 
                   group = country)) %>%
  hc_colors(cols3) %>%
  hc_title(text ="GDP Growth in World's 5 Fastest Growing Economies") %>%
  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")
ggplot(happy5, aes(year,gdp_tn)) + 
  geom_line(aes(y = gdp_tn))

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.
highchart () %>%
  hc_add_series(data = regions,
                   type = "area",
                   hcaes(x = year,
                   y = gdp_tn, 
                   group = region))
regions <- nations %>%
  group_by(year,region) %>%
  summarize(gdp_percap = sum(gdp_percap, na.rm = TRUE)) %>%
  arrange(year,region)
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
highchart () %>%
  hc_add_series(data = regions,
                   type = "area",
                   hcaes(x = year,
                   y = gdp_percap, 
                   group = region))
cols <- brewer.pal(7, "Set2")
highchart () %>%
  hc_add_series(data = regions,
                   type = "area",
                   hcaes(x = year,
                   y = gdp_percap, 
                   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)
summary(happy5$gdp_percap, na.rm = TRUE)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   17023   23746   31727   33559   40438   66817