1 Introduction

This example .Rmd document demonstrates how to embed htmlwidgets in HTML output formats. Note that we’ve specifically used the bookdown::html_document2 format to demonstrate how htmlwidgets can be used with captions.

We need the following packages for this document:

library("gapminder")
library("tidyverse")
library("highcharter")
library("lubridate")
library("glue")
library("leaflet")
library("leaflet.extras")
library("sf")
library("rnaturalearth")

2 Population growth

The visualisation below was created with highcharter. The highcharter library provides the ability to create a wide range of beautiful, professional looking charts.

gapminder_continents <- gapminder %>%
  group_by(year, continent) %>%
  summarise_if(is.numeric, funs(round(mean(as.numeric(.))))) %>%
  ungroup() %>%
  mutate(year = ymd(glue("{year}-12-31")))
  
highchart(type = "stock") %>%
  hc_add_series(data = gapminder_continents,
                type = "line",
                hcaes(x = year,
                      y = pop,
                      group = continent)) %>%
  hc_plotOptions(series = list(marker = list(enabled = TRUE))) %>%
  hc_tooltip(xDateFormat = "%Y") %>%
  hc_legend(enabled = TRUE, reverse = TRUE)

Figure 2.1: Continental population growth (Source: Gapminder)

3 GDP Choropleth

The leaflet library allows us to create interactive maps, including shaded-area maps (choropleth) like the one below.

countries_sf <- countries110 %>%
  st_as_sf()

pop_palette <- colorNumeric("viridis",
                            domain = countries_sf$gdp_md_est)

lf_choropleth <- countries_sf %>%
  filter(! name == "Antarctica") %>%
  leaflet() %>%
  addPolygons(fillColor = ~pop_palette(gdp_md_est),
              color = "black",
              weight = 1,
              opacity = 1,
              fillOpacity = 0.7,
              popup = ~glue("<h3>Country: {name}", "<br>",
                           "Population: {pop_est}",
                           "<br>",
                           "GPD: {gdp_md_est}</h3>")) %>%
  addLegend(pal = pop_palette,
            values = ~gdp_md_est,
            opacity = 0.7,
            title = "GDP in $ (2009)") %>%
  setMapWidgetStyle(style = list(background = "#aacbff"))
widgetframe::frameWidget(lf_choropleth)

Figure 3.1: Choropleth showing per country GDP (Total $ in 2009)