Skip to contents

hchart() chooses a chart based on the class of its input. Find your object type below, then use the hc_* functions to customize the resulting chart.

Data objects

Data frames

For a data frame, choose the chart type with type and map columns to x and y with hcaes(). In the scatter plot below, group = species creates a separate series for each penguin species.

library(highcharter)
library(dplyr)

hchart(
  palmerpenguins::penguins,
  "scatter",
  hcaes(x = body_mass_g, y = flipper_length_mm, group = species)
)
penguins2 <- palmerpenguins::penguins |>
  count(species, island)

hchart(penguins2, "column", hcaes(x = island, y = n, group = species))

Check automatically if the x column is date class:

economics_long2 <- ggplot2::economics_long |>
  filter(variable %in% c("pop", "uempmed", "unemploy"))

hchart(economics_long2, "line", hcaes(x = date, y = value01, group = variable))

Numeric & histograms

set.seed(123)
x <- rgamma(1000, shape = 2)
hchart(x)

Densities

hchart(density(x), type = "area", color = "#B71C1C", name = "Gamma sample")

Empirical cumulative distributions

The height at each x-value is the fraction of the sample at or below it. For example, 626 of the 1,000 observations are at or below 2.

hchart(ecdf(x), name = "Gamma sample")

Precomputed histograms

Pass a histogram object when you want R to choose the breaks before charting. The bin boundaries and counts are preserved.

hchart(hist(x, breaks = 30, plot = FALSE), name = "Gamma sample")

Character & factor

hchart(ggplot2::diamonds$cut, type = "column")

Frequency tables

Each cell shows how many cars have that combination of cylinders and gears. Zero counts remain visible in the heatmap. For a one-way table, see Category order.

hchart(table(Cylinders = mtcars$cyl, Gears = mtcars$gear))

Matrix

The volcano matrix has 87 rows and 61 columns. Both views use the same distance for one step across a column or down a row.

volcano_layout <- function(chart) {
  chart |>
    hc_chart(margin = c(0, 96, 0, 0)) |>
    hc_legend(layout = "vertical", align = "right", verticalAlign = "middle")
}
# fmt: skip
volcano_colors <- c("#000004", "#56106E", "#BB3754", "#F98C0A", "#FCFFA4")

hchart(volcano) |>
  volcano_layout() |>
  hc_colorAxis(stops = color_stops(colors = volcano_colors))

The elevations in volcano are whole metres. Place contour lines half a metre above each multiple of ten so that flat areas keep their band color. Blue, yellow and coral mark increasing elevation, as in the Highcharts contour demo. This view requires WebGPU in the browser.

hchart(
  volcano,
  type = "contour",
  contourInterval = 10,
  contourOffset = 0.5,
  lineColor = "#888888"
) |>
  volcano_layout() |>
  hc_colorAxis(
    min = min(volcano),
    max = max(volcano),
    startOnTick = FALSE,
    endOnTick = FALSE,
    stops = color_stops(3, c("#447CFF", "#F5FF66", "#FF5E4F")),
    labels = list(format = "{value} m")
  )

Distance matrix

hchart(dist(mtcars[1:20, ]))

Correlation matrix

hchart(cor(mtcars))

Principal components

Each point represents one state. Axis titles give the share of variance explained by each component. Hover over a point to see the state’s name.

Use loadings = TRUE to show how the variables relate to the chosen components. Use scale = 0 for the raw observation scores.

fit <- prcomp(USArrests, scale. = TRUE)
hchart(fit, choices = c(1, 3), loadings = TRUE)

GeoJSON

Manchester Airport is a single GeoJSON FeatureCollection. Its 26 polygons outline terminals and aprons, 254 lines trace runways and taxiways, and 136 points mark gates and other airfield features. Pass the collection to hchart() to draw the three layers. Hover over a gate to see its reference, such as Gate 21.

path <- system.file(
  "extdata",
  "manchester-airport.geojson",
  package = "highcharter"
)
airport <- geojsonio::geojson_list(geojsonio::as.json(path))
hchart(airport, name = c("Areas", "Lines", "Points")) |>
  hc_tooltip(headerFormat = "", pointFormat = "<b>{point.name}</b>") |>
  hc_mapNavigation(enabled = TRUE, enableMouseWheelZoom = FALSE)

The map shows the three geometry layers over a local MapLibre basemap. Toggle a layer in the legend or use the zoom buttons to explore. The GeoJSON preserves named features and labels the others with their airfield category and reference where available. Airport data from POSCON UK · Basemap example code.

The Maps vignette shows how to style these geometry types separately on a racing circuit.

Time series

hchart(LakeHuron, name = "Level") |>
  hc_title(text = "Level of Lake Huron 1875–1972")

Multivariate time series

hchart(cbind(mdeaths, fdeaths))

Seasonal decomposition

The four panels share a time axis and show the observed series, seasonality, trend and remainder. Gaps at the ends of the trend are part of the fitted decomposition.

hchart(stats::decompose(AirPassengers, type = "multiplicative"))

Seasonal decomposition of time series by loess

hchart(stl(log(AirPassengers), "per"))

Seasonal adjustment with X-13

With the optional seasonal package installed, compare the original monthly series with the X-13-adjusted series on the same scale. Labels mark the outliers reported by the model at the adjusted value, as in plot(fit) (e.g., AO for an additive outlier or LS for a level shift).

hchart(seasonal::seas(AirPassengers))

Causal impact

A simulated campaign lifts daily sales by 10 units from day 71 onward. An unaffected control series helps estimate what sales would have been without the campaign. The top panel compares observed sales with that counterfactual. The next two show the estimated daily lift and its running total. Shaded bands show the posterior intervals.

set.seed(274)
dates <- as.Date("2025-01-01") + 0:99
control <- 50 + cumsum(rnorm(100))
sales <- 2 * control + rnorm(100, sd = 2) + 10 * (dates >= dates[71])
impact <- CausalImpact::CausalImpact(
  zoo::zoo(cbind(sales, control), dates),
  pre.period = dates[c(1, 70)],
  post.period = dates[c(71, 100)]
)
hchart(impact)

Forecast package

The legend distinguishes the observed series from the forecast. Prediction intervals are linked to the forecast series. Supply name to label the subject; the chart adds the observed/forecast suffixes.

fit <- forecast::ets(USAccDeaths)
hchart(forecast::forecast(fit, h = 48, level = 95))

Exponential-smoothing components

An ets model shows its observed values and fitted level, slope or seasonal states in aligned panels. To show future values instead, pass the model to forecast() and chart the resulting forecast object.

hchart(forecast::ets(AirPassengers))

Autocovariance & autocorrelation

changes <- diff(AirPassengers)
hchart(acf(changes, plot = FALSE))
hchart(pacf(changes, plot = FALSE))

With two monthly series, the diagonal panels show each series’ autocorrelation. The other two panels show their cross-correlation at positive and negative lags. The horizontal axes use years, the time unit of these monthly series.

hchart(acf(cbind(mdeaths, fdeaths), lag.max = 12, plot = FALSE))

Stock prices

An xts series with open, high, low and close prices produces a Stock candlestick chart.

data(sample_matrix, package = "xts")
hchart(xts::as.xts(sample_matrix))

Fitted models

Random forests

Can a car’s length, height and curb weight distinguish two-door models from four-door models? Hover at a tree count to compare the overall out-of-bag error with the error for each class.

data("imports85", package = "randomForest")
set.seed(42)
forest <- randomForest::randomForest(
  numOfDoors ~ length + height + curbWeight,
  data = imports85,
  na.action = na.omit,
  ntree = 150
)
hchart(forest)

Cross-validated regularization

Each point shows mean cross-validated prediction error. Its whiskers extend one standard error in either direction. The two vertical lines identify the best-scoring lambda and the more regularized one-standard-error choice. Move the horizontal crosshair to an interval endpoint to see which other intervals reach that value.

x <- as.matrix(mtcars[c("disp", "hp", "wt", "qsec")])
set.seed(42)
folds <- sample(rep_len(1:5, nrow(x)))
cv <- glmnet::cv.glmnet(x, mtcars$mpg, foldid = folds)
hchart(cv)

Survival package

Survival models can be charted.

library(survival)

fit <- survfit(Surv(time, status) ~ sex, data = survival::cancer)

hchart(fit, ranges = TRUE)

Decision trees

Node area shows how many training observations reach each split or leaf. For classification, select a donut or a bar to see the class proportions. For regression, the node color follows the mean response. Hover over a node for its summary, or click a split to fold the subtree.

fit <- partykit::ctree(Species ~ Petal.Length + Petal.Width, data = iris)
hchart(fit, node_shape = "bar") |>
  hc_colors(c("#087B71", "#AD591D", "#7655A0"))

Boosted trees

Select a tree by its one-based index. Hover over a split for its gain and the direction taken by missing values, or over a leaf for its margin contribution. Marker size follows cover.

x <- as.matrix(iris[c("Sepal.Length", "Sepal.Width", "Petal.Length")])
d <- xgboost::xgb.DMatrix(x, label = iris$Petal.Width)
fit <- xgboost::xgb.train(
  list(objective = "reg:squarederror", max_depth = 2L, nthread = 1L),
  d,
  nrounds = 3L,
  verbose = 0
)
hchart(fit, tree = 2)

Dendrograms and phylogenies

Both examples show the same nine families from the bird phylogeny in ape.

data("bird.families", package = "ape")
families <- c(
  "Rheidae",
  "Casuariidae",
  "Struthionidae",
  "Phasianidae",
  "Numididae",
  "Odontophoridae",
  "Anhimidae",
  "Dendrocygnidae",
  "Anatidae"
)
birds <- ape::keep.tip(bird.families, families)

Cluster dendrograms

The tree is ultrametric, so all tips are the same distance from the root. In its dendrogram, the height of each join gives the distance between tips on opposite branches of that join. Hover over a join to see how many families it contains. Both hchart(fit) and hchart(as.dendrogram(fit)) use this layout.

htmltools::tags$section(
  htmltools::tags$button(
    "Change branches",
    type = "button",
    onclick = "
    const panel = this.closest('section');
    const chart = Highcharts.charts.find(c => c && panel.contains(c.renderTo));
    const series = chart.series[0], styles = ['orthogonal', 'curved', 'straight'];
    const next = (styles.indexOf(series.options.link.type) + 1) % styles.length;
    series.update({ link: { type: styles[next] } });
  "
  ),
  hchart(as.hclust(birds))
)

Phylogenies

In the fan layout, radial distance measures branch length from the root. With no lengths, each edge counts as one step. Use layout = "rectangular" for a side-by-side reading of tip labels.

hchart(birds)

Networks

library(igraph)
N <- 40

net <- sample_gnp(N, p = 2 / N)
wc <- cluster_walktrap(net)

V(net)$label <- seq(N)
V(net)$name <- paste("I'm #", seq(N))
V(net)$page_rank <- round(page_rank(net)$vector, 2)
V(net)$betweenness <- round(betweenness(net), 2)
V(net)$degree <- degree(net)
V(net)$size <- V(net)$degree
V(net)$comm <- as.vector(membership(wc))
V(net)$color <- colorize(membership(wc))

hchart(net, layout = layout_with_fr)