Skip to contents

This a generic function, this means you can chart various R like numeric, histograms, character, factor, ts on the fly. The resulting chart is a highchart object so you can keep modifying with the implemented API.

Data Frames

This function works like qplot: You pass the data, choose the type of chart and then define the aesthetics for each variable.

library(highcharter)
library(dplyr)

data(penguins, package = "palmerpenguins")
data(diamonds, economics_long, package = "ggplot2")

hchart(penguins, "scatter", hcaes(x = body_mass_g, y = flipper_length_mm , group = species))
penguins2 <- penguins |>
  count(species, island) |>
  glimpse()
## Rows: 5
## Columns: 3
## $ species <fct> Adelie, Adelie, Adelie, Chinstrap, Gentoo
## $ island  <fct> Biscoe, Dream, Torgersen, Dream, Biscoe
## $ n       <int> 44, 56, 52, 68, 124
hchart(penguins2, "column", hcaes(x = island, y = n, group = species))

Check automatically if the x column is date class:

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

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

Numeric & Histograms

x <- diamonds$price
hchart(x)

Densities

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

Character & Factor

x <- diamonds$cut
hchart(x, type = "column")

Time Series

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

Seasonal Decomposition of Time Series by Loess

x <- stl(log(AirPassengers), "per")
hchart(x)

Forecast package

library(forecast)

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

Igraph package

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 <- membership(wc)
V(net)$color <- colorize(membership(wc))

hchart(net, layout = layout_with_fr)

Survival Package

Survival models can be charted.

library(survival)

data(cancer, package = "survival")

lung <- dplyr::mutate(cancer, sex = ifelse(sex == 1, "Male", "Female"))

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

hchart(fit, ranges = TRUE)

Quantmod package

The highstock extension is used to chart xts and xts ohlc classes from the quantmod package.

library(quantmod)

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

hchart(x)

Multivariate Time series

x <- cbind(mdeaths, fdeaths)
hchart(x)

Autocovariance & Autocorrelation

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

Principal Components

hchart(princomp(USArrests, cor = TRUE))

Matrix

data(volcano)

hchart(volcano) |> # changing default color
  hc_colorAxis(
    stops = color_stops(colors = c("#000004FF", "#56106EFF", "#BB3754FF", "#F98C0AFF", "#FCFFA4FF"))
    )

Distance matrix

mtcars2 <- mtcars[1:20, ]
x <- dist(mtcars2)
hchart(x)

Correlation matrix

hchart(cor(mtcars))