How to Visualize Data With Highcharter

April 2, 2018
By

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

INTRODUCTION

Highcharter is a R wrapper for Highcharts javascript libray and its modules. Highcharts is very mature and flexible javascript charting library and it has a great and powerful API1.

The main features of this package are:

Various chart type with the same style: scatters, bubble, line, time series, heatmaps, treemap, bar charts, networks.

Chart various R object with one function. With hchart(x) you can chart: data.frames, numeric, histogram, character, density, factors, ts, mts, xts, stl, ohlc, acf, forecast, mforecast, ets, igraph, dist, dendrogram, phylo, survfit classes.

Support Highstock charts. You can create a candlestick charts in 2 lines of code. Support xts objects from the quantmod package.

Support Highmaps charts. It’s easy to create choropleths or add information in geojson format.

Piping styling.

Themes: you configurate your chart in multiples ways. There are implemented themes like economist, financial times, google, 538 among others.

Plugins: motion, drag points, fontawesome, url-pattern, annotations.

Installation

install.packages("highcharter")
library(highcharter)

Basic Example

This is a simple example using hchart function.

library("highcharter")
data(diamonds, mpg, package = "ggplot2")

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

Learn more about using different visualization packages in the online course R: Complete Data Visualization Solutions. In this course, you will learn how to:

  • Work extensively with the ggplot package and its functionality
  • Learn what visualizations exist for your specific use case
  • And much more

The highcharts API

highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "A highcharter chart") %>%
hc_xAxis(categories = 2012:2016) %>%
hc_add_series(data = c(3900, 4200, 5700, 8500, 11900),
name = "Downloads")

Generic Function hchart

Among its features highcharter can chart various objects depending of its class with the generic2 hchart function.

hchart(diamonds$cut, colorByPoint = TRUE, name = "Cut")

Zoom

hchart(diamonds$price, color = "#B71C1C", name = "Price") %>%
hc_title(text = "You can zoom me")

Forecasting

One of the nicest class which hchart can plot is the forecast class from the forecast package.

library("forecast")

airforecast <- forecast(auto.arima(AirPassengers), level = 95)

hchart(airforecast)

Highstock

With highcharter you can use the highstock library which include sophisticated navigation options like a small navigator series, preset date ranges, date picker, scrolling and panning. With highcarter it’s easy make candlesticks or ohlc charts using time series data. For example data from quantmod package.

library("quantmod")

x <- getSymbols(“GOOG”, auto.assign = FALSE)
y %
hc_add_series(x) %>%
hc_add_series(y, type = “ohlc”)

Highmaps

You can chart maps and choropleth using the highmaps module.

data(unemployment)

hcmap("countries/us/us-all-all", data = unemployment,
name = "Unemployment", value = "value", joinBy = c("hc-key", "code"),
borderColor = "transparent") %>%
hc_colorAxis(dataClasses = color_classes(c(seq(0, 10, by = 2), 50))) %>%
hc_legend(layout = "vertical", align = "right",
floating = TRUE, valueDecimals = 0, valueSuffix = "%")

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)