10/28

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(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.1     ✔ readr     2.2.0
✔ ggplot2   4.0.3     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.1     ✔ tidyr     1.3.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(highcharter)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
library(RColorBrewer)
nations <- read_csv("nations.csv") |>
  mutate(gdp_tn = gdp_percap*population)
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.
str(nations)
tibble [5,275 × 11] (S3: tbl_df/tbl/data.frame)
 $ iso2c             : chr [1:5275] "AD" "AD" "AD" "AD" ...
 $ iso3c             : chr [1:5275] "AND" "AND" "AND" "AND" ...
 $ country           : chr [1:5275] "Andorra" "Andorra" "Andorra" "Andorra" ...
 $ year              : num [1:5275] 1996 1994 2003 1990 2009 ...
 $ gdp_percap        : num [1:5275] NA NA NA NA NA NA NA NA NA NA ...
 $ population        : num [1:5275] 64291 62707 74783 54511 85474 ...
 $ birth_rate        : num [1:5275] 10.9 10.9 10.3 11.9 9.9 NA 10.9 9.8 11.8 11.2 ...
 $ neonat_mortal_rate: num [1:5275] 2.8 3.2 2 4.3 1.7 1.6 2 1.7 2.1 2.1 ...
 $ region            : chr [1:5275] "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" ...
 $ income            : chr [1:5275] "High income" "High income" "High income" "High income" ...
 $ gdp_tn            : num [1:5275] NA NA NA NA NA NA NA NA NA NA ...
# prepare data
big4 <- nations |>
  filter(iso3c %in% c("CHN","DEU", "JPN", "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 = "left", 
            verticalAlign = "top")
# prepare data
regions <- nations |>
  group_by(year,region) |>
  summarize(gdp_tn = sum(gdp_tn, na.rm = TRUE)) |>
  arrange(year,region)
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by year and region.
ℹ Output is grouped by year.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(year, region))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
# basic area chart, default options
highchart () |>
  hc_add_series(data = regions,
                   type = "area",
                   hcaes(x = year,
                   y = gdp_tn, 
                   group = region))
# 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") 
nationsBirths <- nations %>%
  mutate(births = (birth_rate/1000)*population) %>%
  filter(iso3c %in% c("DEU", "NGA", "CHN","IND"))
ggplot(nationsBirths, aes(x = year, y = births, color = country)) + geom_point() + geom_line() + scale_color_brewer(palette = "Set1")

highchart() |>
  hc_add_series(data = nationsBirths,
                   type = "line", 
                hcaes(x = year,
                      y = births, 
                      group = country))
regionalBirths <- nations %>%
  mutate(births = (birth_rate/1000)*population) %>%
  group_by(region, year) %>%
  summarise(
    totalBirths = sum(births, na.rm = TRUE), 
    .groups = "drop"
  )
ggplot(regionalBirths, aes(x = year, y = totalBirths, fill = region)) +
  geom_area(color = "white", linewidth = 0.1) +
  scale_fill_brewer(palette = "Set2")

highchart () |>
  hc_add_series(data = regionalBirths,
                   type = "area",
                   hcaes(x = year,
                   y = totalBirths, 
                   group = region))
cols <- brewer.pal(7, "Set2")

# stacked area chart
highchart () |>
  hc_add_series(data = regionalBirths,
                   type = "area",
                   hcaes(x = year,
                   y = totalBirths, 
                   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="Births")) |>
  hc_legend(align = "right", verticalAlign = "top",
            layout = "vertical")