Disease and Democracy

Author

Grigory Balasanov

Disease and Democracy

Load and Process Data

# Loading libraries

library(tidyverse)
library(highcharter)
library(RColorBrewer)
library(dplyr)



#setwd("C:/Users/rsaidi/Dropbox/Rachel/MontColl/Datasets/Datasets")
nations <- read_csv("../datasets/nations.csv") |> 
  mutate(gdp_tn = gdp_percap*population/10^12) # gdp_tn - for Trillion

#head(nations)

Make a version of the “China’s rise” chart from unit 3 assignment

# prepare data
big4 <- nations |>
  filter(iso3c %in% c("CHN","DEU", "JPN", "USA")) |>
  arrange(year)

The arrange step is important, because highcharter needs the data in order when drawing a time series - otherwise any line drawn through the data will follow the path of the data order, not the correct time order.

Now draw a basic chart with default settings:

# install highcharter, RColorBrewer
# load required packages
#install.packages("RColorBrewer")

# basic symbol-and-line chart, default settings
highchart() |>
  hc_add_series(data = big4,
                type = "line", 
                hcaes(x = year,
                      y = gdp_tn, 
                      group = country))

Use a ColorBrewer palette

Using RColorBrewer, we can set a palette, and then use it in highcharter.

# 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)

Change the legend position

For this, we use the hc_legend function.

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

By default we have a tooltip for each series, or line, and the numbers run to many decimal places.

We can change to one tooltip for each year with “shared = TRUE”, and round all the numbers to two decimal places with pointFormat = “{point.country}: {point.gdp_tn:.2f}

# 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}")
big4_chart

Prepare the data

First, prepare the data using dplyr.

# prepare data
regions <- nations |>
  group_by(year,region) |>
  summarize(gdp_tn = sum(gdp_tn, na.rm = TRUE)) |>
  arrange(year,region)

Make an area chart using default options

# basic area chart, default options
highchart () |>
  hc_add_series(data = regions,
                type = "area",
                hcaes(x = year,
                      y = gdp_tn, 
                      group = region))

This is an area chart, but the areas are plotted over one another, rather than stacked.

The following code fixes that, and customizes the chart in other ways. It uses the same ColorBrewer palette, with seven colors, that we used in unit 3.

# 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") 

Food Stamps Data - Combine Two Types

#setwd("C:/Users/rsaidi/Dropbox/Rachel/MontColl/Datasets/Datasets")
food_stamps<- read_csv("../datasets/food_stamps.csv")
Rows: 47 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (3): year, participants, costs

ℹ 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.
cols <- c("dodgerblue","orange")  # set the color palette to dodger blue and orange

highchart() |>
  hc_yAxis_multiples(
    list(title = list(text = "Participants (millions)")),
    list(title = list(text = "Costs ($ billions)"),
         opposite = TRUE)
  ) |>
  hc_add_series(data = food_stamps$participants,
                name = "Participants (millions)",
                type = "column",
                yAxis = 0) |>
  hc_add_series(data = food_stamps$costs,
                name = "Costs ($ billions)",
                type = "line",
                yAxis = 1) |>
  hc_xAxis(categories = food_stamps$year,
           tickInterval = 5) |>
  hc_colors(cols) |>
  hc_chart(style = list(fontFamily = "AvantGarde",
                        fontWeight = "bold"))

CLASS ACTIVITY FOR SUBMISSION

China’s Rise to Become the Largest Economy

big4 <- nations |>
  filter(country %in% c("United States", "China", "Japan", "Germany"))


ggplot(big4, aes(x = year, y = gdp_tn, color = country)) +
  geom_line() +
  geom_point() +
  scale_color_brewer(palette = "Set1") +
  labs(title = "China's Rise to Become the Largest Economy",
       x = "Year",
       y = "GDP ($ trillion)",
       color = "Country") +
  theme_minimal()

GDP by World Bank Region

world_bank_regions <- nations |>
  group_by(region, year) |>
  summarise(GDP = sum(gdp_tn, na.rm = TRUE))
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by region and year.
ℹ Output is grouped by region.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(region, year))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
# Creating a chart
ggplot(world_bank_regions, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", linewidth = 0.2) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "GDP by World Bank Region",
       x = "Year",
       y = "GDP ($ trillion)",
       fill = "Region") +
  theme_minimal()