library(highcharter) options(highcharter.theme = hc_theme_hcrt(tooltip = list(valueDecimals = 2))) options(download.file.method = "libcurl")
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.
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() 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))
x <- diamonds$price hchart(x)
hchart(density(x), type = "area", color = "#B71C1C", name = "Price")
x <- diamonds$cut hchart(x, type = "column")
hchart(LakeHuron, name = "Level") |> hc_title(text = "Level of Lake Huron 1875–1972")
x <- stl(log(AirPassengers), "per") hchart(x)
library(forecast) x <- forecast(ets(USAccDeaths), h = 48, level = 95) hchart(x)
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 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)
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)
x <- cbind(mdeaths, fdeaths) hchart(x)
x <- acf(diff(AirPassengers), plot = FALSE) hchart(x)
hchart(princomp(USArrests, cor = TRUE))
data(volcano) hchart(volcano) |> # changing default color hc_colorAxis( stops = color_stops(colors = c("#000004FF", "#56106EFF", "#BB3754FF", "#F98C0AFF", "#FCFFA4FF")) )
mtcars2 <- mtcars[1:20, ] x <- dist(mtcars2) hchart(x)
hchart(cor(mtcars))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.