Presenting Highcharter

January 13, 2016
By

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

After a lot of documentation, a lot of R CMD checks and a lot of patience from CRAN
people I'm happy to anonounce highcharter v0.1.0:
A(nother) wrapper for Highcharts charting library.

Now it's easy make a chart like this. Do you want to know how?

presentinghighcharts

I like Highcharts. It was the
first charting javascript library what I used in a long time and have
very a mature API to plot a lot of types of charts. Obviously there are some R
packages to plot data using this library:

  • Ramnath Vaidyanathan's rCharts.
    What a library. This was the beginning to the R & JS romance. The rCharts approach to plot data
    is object oriented; here we used a lot of chart$Key(arguments, ...).
  • highchartR package from jcizel.
    This package we use the highcharts function and give some parameters, like the variable's names
    to get the chart.

With these package you can plot almost anything, so why another wrapper/package/ for highcharts?
The main reasons were/are:

  • Write/code highcharts plots using the piping style and get similar results like
    dygraphs, metricsgraphics,
    taucharts, leaflet
    packages.
  • Get all the funcionalities from the highcharts' API. This means
    make a not so useR-friendly wrapper in the sense that you maybe need to make/construct
    the chart specifying paramter by parameter (just like highcharts). But don't worry, there are
    some shortcuts functions to plot some R objects on the fly (see the examples below).
  • Include and create themes :D.
  • Put all my love for highcharts in somewhere.

Some Q&A

When use this package? I recommend use this when you have fihinsh your analysis and you want
to show your result with some interactivity. So, before use experimental plot to visualize, explore
the data with ggplot2 then use highcharter as one of the various alternatives what we have
in ouR community like ggivs, dygraphs, taucharts, metricsgraphics, plotly among others
(please check hafen's htmlwidgets gallery)

What are the advantages of using this package? Basically the advantages are inherited from
highcharts: have a numerous chart types with the same format, style, flavour. I think in a
situation when you use a treemap and a scatter chart from differents packages in a shiny app.
Other advantage is the posibility to create or modify themes (I like this a lot!) and
customize in every way your chart: beatiful tooltips, titles, credits, legends, add plotlines or
plotbands
.

What are the disadvantages of this package and highcharts? One thing I miss is the facet
implementation like in taucharts and hrbrmstr's taucharts.
This is not really necesary but it's really good when a visualization library has it. Maybe
other disadvantage of this implmentation is the functions use standar evaluation
plot(data$x, data$y) instead something more direct like plot(data, ~x, ~y). That's why
I recommed this package to make the final chart instead use the package to explorer visually
the data.

The Hellow World chart

Let's see a simple chart.

library("highcharter")
library("magrittr")
library("dplyr")

data("citytemp")

citytemp
month tokyo new_york berlin london
Jan 7.0 -0.2 -0.9 3.9
Feb 6.9 0.8 0.6 4.2
Mar 9.5 5.7 3.5 5.7
Apr 14.5 11.3 8.4 8.5
May 18.2 17.0 13.5 11.9
Jun 21.5 22.0 17.0 15.2
Jul 25.2 24.8 18.6 17.0
Aug 26.5 24.1 17.9 16.6
Sep 23.3 20.1 14.3 14.2
Oct 18.3 14.1 9.0 10.3
Nov 13.9 8.6 3.9 6.6
Dec 9.6 2.5 1.0 4.8
hc <- highchart() %>% 
  hc_add_serie(name = "tokyo", data = citytemp$tokyo)

hc

Very simple chart. Here comes the powerful highchart API: Adding more series
data and adding themes.

hc <- hc %>% 
  hc_title(text = "Temperatures for some cities") %>% 
  hc_xAxis(categories = citytemp$month) %>% 
  hc_add_serie(name = "London", data = citytemp$london,
               dataLabels = list(enabled = TRUE)) %>%
  hc_add_serie(name = "New York", data = citytemp$new_york,
               type = "spline") %>% 
  hc_yAxis(title = list(text = "Temperature"),
           labels = list(format = "{value}? C")) %>%
  hc_add_theme(hc_theme_sandsignika())

hc

Now, what we can do with a little extra effort:

library("httr")
library("purrr")

# get some data
swmovies <- content(GET("http://swapi.co/api/films/?format=json"))

swdata <- map_df(swmovies$results, function(x){
  data_frame(title = x$title,
             species = length(x$species),
             planets = length(x$planets),
             release = x$release_date)
}) %>% arrange(release)

swdata 
title species planets release
A New Hope 5 3 1977-05-25
The Empire Strikes Back 5 4 1980-05-17
Return of the Jedi 9 5 1983-05-25
The Phantom Menace 20 3 1999-05-19
Attack of the Clones 14 5 2002-05-16
Revenge of the Sith 20 13 2005-05-19
The Force Awakens 3 1 2015-12-11
# made a theme
swthm <- hc_theme_merge(
  hc_theme_darkunica(),
  hc_theme(
    credits = list(
      style = list(
        color = "#4bd5ee"
      )
    ),
    title = list(
      style = list(
        color = "#4bd5ee"
        )
      ),
    chart = list(
      backgroundColor = "transparent",
      divBackgroundImage = "http://www.wired.com/images_blogs/underwire/2013/02/xwing-bg.gif",
      style = list(fontFamily = "Lato")
    )
  )
)

# chart
highchart() %>% 
  hc_add_theme(swthm) %>% 
  hc_xAxis(categories = swdata$title,
           title = list(text = "Movie")) %>% 
  hc_yAxis(title = list(text = "Number")) %>% 
  hc_add_serie(data = swdata$species, name = "Species",
               type = "column", color = "#e5b13a") %>% 
  hc_add_serie(data = swdata$planets, name = "Planets",
               type = "column", color = "#4bd5ee") %>%
  hc_title(text = "Diversity in <span style="color:#e5b13a">
           STAR WARS</span> movies",
           useHTML = TRUE) %>% 
  hc_credits(enabled = TRUE, text = "Source: SWAPI",
             href = "https://swapi.co/",
             style = list(fontSize = "12px"))

More Examples

For ts objects. Compare this example with the dygrapths
one

highchart() %>% 
  hc_title(text = "Monthly Deaths from Lung Diseases in the UK") %>% 
  hc_add_serie_ts2(fdeaths, name = "Female") %>%
  hc_add_serie_ts2(mdeaths, name = "Male") 

A more elaborated example using the mtcars data. And it's nice like
juba's scatterD3.

hcmtcars <- highchart() %>% 
  hc_title(text = "Motor Trend Car Road Tests") %>% 
  hc_subtitle(text = "Source: 1974 Motor Trend US magazine") %>% 
  hc_xAxis(title = list(text = "Weight")) %>% 
  hc_yAxis(title = list(text = "Miles/gallon")) %>% 
  hc_chart(zoomType = "xy") %>% 
  hc_add_serie_scatter(mtcars$wt, mtcars$mpg,
                       mtcars$drat, mtcars$hp,
                       rownames(mtcars),
                       dataLabels = list(
                         enabled = TRUE,
                         format = "{point.label}"
                       )) %>% 
  hc_tooltip(useHTML = TRUE,
             headerFormat = "<table>",
             pointFormat = paste("<tr><th colspan="1"><b>{point.label}</b></th></tr>",
                                 "<tr><th>Weight</th><td>{point.x} lb/1000</td></tr>",
                                 "<tr><th>MPG</th><td>{point.y} mpg</td></tr>",
                                 "<tr><th>Drat</th><td>{point.z} </td></tr>",
                                 "<tr><th>HP</th><td>{point.valuecolor} hp</td></tr>"),
             footerFormat = "</table>")
hcmtcars

Let's try treemaps

library("treemap")
library("viridisLite")

data(GNI2010)
tm <- treemap(GNI2010, index = c("continent", "iso3"),
              vSize = "population", vColor = "GNI",
              type = "value", palette = viridis(6))


hc_tm <- highchart() %>% 
  hc_add_serie_treemap(tm, allowDrillToNode = TRUE,
                       layoutAlgorithm = "squarified",
                       name = "tmdata") %>% 
  hc_title(text = "Gross National Income World Data") %>% 
  hc_tooltip(pointFormat = "<b>{point.name}</b>:<br>
             Pop: {point.value:,.0f}<br>
             GNI: {point.valuecolor:,.0f}")

hc_tm

You can do anything

As uncle Bem said some day:

SavePie

You can use this pacakge for evil purposes so be good with the people who see
your charts. So, I will not be happy if I see one chart like this:

iriscount <- count(iris, Species)
iriscount
Species n
setosa 50
versicolor 50
virginica 50
highchart(width = 400, height = 400) %>% 
  hc_title(text = "Nom! a delicious 3d pie!") %>%
  hc_subtitle(text = "your eyes hurt?") %>% 
  hc_chart(type = "pie", options3d = list(enabled = TRUE, alpha = 70, beta = 0)) %>% 
  hc_plotOptions(pie = list(depth = 70)) %>% 
  hc_add_serie_labels_values(iriscount$Species, iriscount$n) %>% 
  hc_add_theme(hc_theme(
    chart = list(
      backgroundColor = NULL,
      divBackgroundImage = "https://media.giphy.com/media/Yy26NRbpB9lDi/giphy.gif"
    )
  ))

Other charts just for charting

data("favorite_bars")
data("favorite_pies")

highchart() %>% 
  hc_title(text = "This is a bar graph describing my favorite pies
           including a pie chart describing my favorite bars") %>%
  hc_subtitle(text = "In percentage of tastiness and awesomeness") %>% 
  hc_add_serie_labels_values(favorite_pies$pie, favorite_pies$percent, name = "Pie",
                             colorByPoint = TRUE, type = "column") %>% 
  hc_add_serie_labels_values(favorite_bars$bar, favorite_bars$percent, type = "pie",
                             name = "Bar", colorByPoint = TRUE, center = c('35%', '10%'),
                             size = 100, dataLabels = list(enabled = FALSE)) %>% 
  hc_yAxis(title = list(text = "percentage of tastiness"),
           labels = list(format = "{value}%"), max = 100) %>% 
  hc_xAxis(categories = favorite_pies$pie) %>% 
  hc_credits(enabled = TRUE, text = "Source (plz click here!)",
             href = "https://www.youtube.com/watch?v=f_J8QU1m0Ng",
             style = list(fontSize = "12px")) %>% 
  hc_legend(enabled = FALSE) %>% 
  hc_tooltip(pointFormat = "{point.y}%")

Well, I hope you use, reuse and enjoy this package!

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

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.

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training

datasociety

http://www.eoda.de





ODSC

ODSC

CRC R books series





Six Sigma Online Training









Contact us if you wish to help support R-bloggers, and place your banner here.

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)