hchart function

This generic function can chart various R objects on the fly. The resulting chart is a highchart object so you can keep modifying with the implmented API

Some implemented classes are: numeric, histogram, character, factor, ts, mts, xts (and OHLC), forecast, acf, dist.

Data Frames

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

data(diamonds, economics_long, mpg, package = "ggplot2")
library(dplyr)

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

mpgman2 <- mpg %>% 
  count(class, year) %>% 
  glimpse()
## Observations: 14
## Variables: 3
## $ class <chr> "2seater", "2seater", "compact", "compact", "midsize", "...
## $ year  <int> 1999, 2008, 1999, 2008, 1999, 2008, 1999, 2008, 1999, 20...
## $ n     <int> 2, 3, 25, 22, 20, 21, 6, 5, 16, 17, 19, 16, 29, 33

hchart(mpgman2, "column", hcaes(x = class, y = n, group = year))

mpgman3 <- mpg %>% 
  group_by(manufacturer) %>% 
  summarise(n = n(), unique = length(unique(model))) %>% 
  arrange(-n, -unique) %>% 
  glimpse()
## Observations: 15
## Variables: 3
## $ manufacturer <chr> "dodge", "toyota", "volkswagen", "ford", "chevrol...
## $ n            <int> 37, 34, 27, 25, 19, 18, 14, 14, 13, 9, 8, 5, 4, 4, 3
## $ unique       <int> 4, 6, 4, 4, 4, 3, 2, 2, 3, 1, 1, 1, 1, 1, 1

hchart(mpgman3, "treemap", hcaes(x = manufacturer, value = n, color = unique))

Check automatically if the x column is date class

economics_long2 <- economics_long %>% 
  filter(variable %in% c("pop", "uempmed", "unemploy")) %>% 
  print()
## Source: local data frame [1,722 x 4]
## Groups: variable [3]
## 
##          date variable  value     value01
##        <date>   <fctr>  <dbl>       <dbl>
## 1  1967-07-01      pop 198712 0.000000000
## 2  1967-08-01      pop 198911 0.001628811
## 3  1967-09-01      pop 199113 0.003282177
## 4  1967-10-01      pop 199311 0.004902803
## 5  1967-11-01      pop 199498 0.006433395
## 6  1967-12-01      pop 199657 0.007734807
## 7  1968-01-01      pop 199808 0.008970739
## 8  1968-02-01      pop 199920 0.009887457
## 9  1968-03-01      pop 200056 0.011000614
## 10 1968-04-01      pop 200208 0.012244731
## # ... with 1,712 more rows
hchart(economics_long2, "line", hcaes(x = date, y = value01, group = variable))

Numeric & Histograms

hchart(diamonds$price) 

Densities

hchart(density(diamonds$price), type = "area", color = "#B71C1C", name = "Price")

Character & Factor

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

Time Series

hchart(LakeHuron)

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

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)

xts from quantmod package

library(quantmod)

x <- getSymbols("USD/JPY", src = "oanda", auto.assign = FALSE)
hchart(x)

xts ohlc objects

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

Autocovariance & Autocorrelation

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

Multivariate Time series

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

Survival Models

library(survival)

data(lung)
lung <- mutate(lung, sex = ifelse(sex == 1, "Male", "Female"))
fit <- survfit(Surv(time, status) ~ sex, data = lung) 

hchart(fit, ranges = TRUE)

Principal Components

hchart(princomp(USArrests, cor = TRUE))

Matrix

data(volcano)
hchart(volcano) %>% 
  # changing default color
  hc_colorAxis(stops = color_stops(colors = viridis::inferno(10)))

Distance matrix

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

Correlation matrix

hchart(cor(mtcars))