This example .Rmd document demonstrates how to embed htmlwidgets in HTML output formats. Note that we’ve specifically used the slidy_presentation format to demonstrate the requirement for widgetframe for all htmlwidgets.
We need the following packages for this document:
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)Continental population growth (Source: Gapminder)
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"))
lf_choropleth