Skip to contents

The highcharter package include highstocks libraries from highchartsJS to create stock or general timeline charts. Features sophisticated navigation for high-volume data, user annotations and range selectors.

Basics

Highstock work well with the quantmod package. It’s easy chart symbols using hchart(). Then you can add more series using hc_add_series().

library(quantmod)

x <- getSymbols("GOOG", auto.assign = FALSE)

hchart(x)

Obviously you can use the implemented API functions to edit the chart:

y <- getSymbols("AMZN", auto.assign = FALSE)

hchart(y, type = "ohlc") |> 
  hc_title(text = "This is a Open-high-low-close chart with a custom theme") |> 
  hc_add_theme(hc_theme_db())

Candlestick and OHLC charts

If you want to chart more symbols in you can use the hc_add_series() function. Don’t forget to specify type = "stock" to activate the navigator, range selector and other features of highstock.

hc <- highchart(type = "stock") |> 
  hc_add_series(x, id = 1) |> 
  hc_add_series(y, type = "ohlc", id = 2)

hc

Flags

Previously we used the id parameter. This is necessary to add flags to relate series and flags:

library(dplyr)

set.seed(123)

data_flags <- tibble(
  date = sample(time(x), size = 5),
  title = sprintf("E #%s", seq_along(date)),
  text = sprintf("An interesting event #%s in %s", seq_along(date), date)
)

glimpse(data_flags)
## Rows: 5
## Columns: 3
## $ date  <date> 2016-10-12, 2016-12-20, 2015-11-04, 2009-02-03, 2007-10-10
## $ title <chr> "E #1", "E #2", "E #3", "E #4", "E #5"
## $ text  <chr> "An interesting event #1 in 2016-10-12", "An interesting event …
hc |> 
  hc_add_series(
    data_flags, 
    hcaes(x = date),
    type = "flags", 
    onSeries = 2
    )