Site icon R-bloggers

How to Visualize Data With Highcharter

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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, awesome, 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))

< aside class='stb-icon'>
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 = "%")

Related exercise sets:

  1. Vector exercises
  2. Spatial Data Analysis: Introduction to Raster Processing: Part-3
  3. Power Analysis Tutorial
  4. Explore all our (>1000) R exercises
  5. Find an R course using our R Course Finder directory

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.