library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.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(dplyr)
library(readr)
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("C:/Users/dylan/OneDrive/Documents/Data 110 Summer")
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.
head(nations)
## # A tibble: 6 × 10
## iso2c iso3c country year gdp_percap population birth_rate neonat_mortal_rate
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AD AND Andorra 1996 NA 64291 10.9 2.8
## 2 AD AND Andorra 1994 NA 62707 10.9 3.2
## 3 AD AND Andorra 2003 NA 74783 10.3 2
## 4 AD AND Andorra 1990 NA 54511 11.9 4.3
## 5 AD AND Andorra 2009 NA 85474 9.9 1.7
## 6 AD AND Andorra 2011 NA 82326 NA 1.6
## # ℹ 2 more variables: region <chr>, income <chr>
nations <- nations %>%
mutate(gdp_trillions = (gdp_percap * population) / 10^12)
big4 <- nations %>%
filter(iso3c == "AND" | iso3c == "ARE" | iso3c == "AFG" | iso3c == "ALB") %>%
arrange(year)
cols <- RColorBrewer::brewer.pal(n = 4, name = "Set1")
highchart() %>%
hc_add_series(data = big4,
type = "line",
hcaes(x = year, y = gdp_trillions, group = country)) %>%
hc_colors(cols) %>%
hc_xAxis(title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "GDP ($ trillion)")) %>%
hc_title(text = "GDP in Trillions for Selected Countries")
Second Chart
processed_data <- nations %>%
group_by(region, year) %>%
summarise(GDP = sum(gdp_trillions, na.rm = TRUE)) %>%
ungroup()
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
head(processed_data)
## # A tibble: 6 × 3
## region year GDP
## <chr> <dbl> <dbl>
## 1 East Asia & Pacific 1990 5.52
## 2 East Asia & Pacific 1991 6.03
## 3 East Asia & Pacific 1992 6.50
## 4 East Asia & Pacific 1993 7.04
## 5 East Asia & Pacific 1994 7.64
## 6 East Asia & Pacific 1995 8.29
# set color palette
cols <- brewer.pal(7, "Set2")
# stacked area chart
highchart() %>%
hc_add_series(data = processed_data, type = "area",
mapping = hcaes(x = year, y = GDP, 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")