The highcharter package

The package is free for non-commercial use but requires a license in corporate and government fields.

We can plot a variety of charts and maps that might not be available with plotly and rbokeh. More specifically: treemaps, better treatment of time series and stock charts (the related packages and topics such as autocovariance and autocorrelation), survival functions, sparkline charts. The package comes with over 500 themes (The Economist, Financial Times, Google, etc.). We can have motion charts like plotly, but also drag points like JavaScript libraries.

Consult the demo site.

A quick example

With the mtcars dataset.

library(highcharter)

hchart(mpg, "scatter", hcaes(x = displ, y = hwy, group = class))

Time series and forecasts

With the AirPassengers dataset.

library(forecast)

airforecast <- forecast(auto.arima(AirPassengers), level = 95)

library(highcharter)

hchart(airforecast)

Average temperatures in New York City.

library(highcharter)
data(citytemp)

hc <- highchart() %>% 
  hc_xAxis(categories = citytemp$month) %>% 
  hc_add_series(name = "New York", data = citytemp$new_york) %>% 
  hc_xAxis(title = list(text = "Month"),
           opposite = TRUE,
           minorTickInterval = "auto",
           minorGridLineDashStyle = "LongDashDotDot",
           plotLines = list(
             list(label = list(text = "Mid-year"),
                  color = "#FF0000",
                  width = 2,
                  value = 6))) %>% 
  hc_xAxis(plotBands = list(
             list(from = 9, to = 12, color = "rgba(100, 0, 0, 0.1)",
                  label = list(text = "This is a plotBand")))) 

hc

Treemaps

A federal government budget: revenues and expenses.

library(highcharter)

hchart(b_revenues, "treemap", hcaes(x = Details, value = Budget2016, color = Budget2016))
hchart(b_expenses, "treemap", hcaes(x = Details, value = -Budget2016, color = -Budget2016))

Stock Exchanges

The CAD/USD exchange rate.

suppressPackageStartupMessages(library(quantmod))

x <- getSymbols("CAD/USD", src = "oanda", auto.assign = FALSE)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
library(highcharter)

hchart(x)

Survival analysis

Subscriber churn (deactivation rate) in telecom.

library(survival)

fit <- survfit(Surv(tenure, Churn) ~ Contract, data = telco2)

library(highcharter)

hchart(fit, ranges = TRUE)
library(survival)

cox_model <- coxph(Surv(tenure, Churn) ~ Contract,
                   method = 'efron',
                   data = telco2)

fit_cox_model <- survfit(cox_model)

library(highcharter)

hchart(fit_cox_model, ranges = TRUE)

Component analysis

Each year, the Heritage Foundation along with the Wall Street Journal releases an Index of Economic Freedom where countries are measured with 12 metrics (12 ‘freedoms’).

Each Freedom metrics are percentages; 100 being the highest value:

  1. Property Rights
  2. Judicial Effectiveness
  3. Government Integrity
  4. Tax Burden
  5. Government Spending
  6. Fiscal Health
  7. Business Freedom
  8. Labor Freedom
  9. Monetary Freedom
  10. Trade Freedom
  11. Investment Freedom
  12. Financial Freedom
library(highcharter)

hchart(princomp(liberty12, cor = TRUE))

Splom

With the mtcars dataset.

library(highcharter)

hchart(cor(mtcars))

Map

A choropleth of North America (region are filled with fake data).

library(highcharter)

hcmap("custom/usa-and-canada", 
      data = data_fake, 
      value = "value",
      joinBy = c("hc-a2", "code"), 
      name = "Fake data",
      dataLabels = list(enabled = TRUE, format = '{point.name}'),
      borderColor = "#FAFAFA", borderWidth = 0.1,
      tooltip = list(valueDecimals = 2, valuePrefix = "$", valueSuffix = " USD")) %>% 
  hc_mapNavigation(enabled = TRUE) %>% 
  hc_exporting(enabled = TRUE)

A bubble map with the same fake data.

hcmap("custom/usa-and-canada", 
      data = data_fake, 
      type = "mapbubble",
      value = "value",
      joinBy = c("hc-a2", "code"), 
      name = "Fake data",
      dataLabels = list(enabled = TRUE, format = '{point.name}'),
      borderColor = "#FAFAFA", borderWidth = 0.1,
      tooltip = list(valueDecimals = 2, valuePrefix = "$", valueSuffix = " USD")) %>% 
  hc_mapNavigation(enabled = TRUE)