Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

Interactive visualizations
using R

Thomas Reinholdsson

rCharts

... an R package to create, customize and publish interactive javascript visualizations from R using a familiar lattice style plotting interface

By: Ramnathv Vaidyanathan (aut, cre), Kenton Russel (ctb), Thomas Reinholdsson (ctb)

Polychart

a <- rPlot(mpg ~ wt | am + vs, data = mtcars, type = 'point', color = 'gear')
a

Morris

data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
a <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
  data = econ)
a$set(pointSize = 0, lineWidth = 1)
a

NVD3

hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
a <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, 
  type = 'multiBarChart')
a

xCharts

require(reshape2)
uspexp <- melt(USPersonalExpenditure)
names(uspexp)[1:2] = c('category', 'year')
a <- xPlot(value ~ year, group = 'category', data = uspexp, 
  type = 'line-dotted')
a

Rickshaw

usp = reshape2::melt(USPersonalExpenditure)
a <- Rickshaw$new()
a$layer(value ~ Var2, group = 'Var1', data = usp, type = 'area')
a$set(width = 600, height = 350)
a

rCharts API


Lattice function

library(rCharts)
hPlot(Pulse ~ Height, data = MASS::survey, type = "scatter", group = "Exer")


Chart methods

library(rCharts)
a <- Highcharts$new()
a$chart(type = "line")
a$title(text = "Monthly Average Rainfall")
...

... similar to the original JS chart api:s

highcharts({
  chart: {
    type: 'column'
  },
  title: {
      text: 'Monthly Average Rainfall'
  },
  ...

Why JavaScript?

All included JavaScript libraries uses JSON to set chart properties, add data, theme settings, etc.

Thanks to RJSONIO/rjson it's easy to convert between R lists and JSON.

Package structure

- Layouts

- JavaScript dependencies

- R code (ReferenceClasses)

Layouts (template)

inst/libraries/highcharts/layout/chart.html

<script type='text/javascript'>
    (function($){
        $(function () {
            var chart = new Highcharts.Chart();
        });
    })(jQuery);
</script>

JavaScript dependencies

inst/libraries/highcharts/config.yml

highcharts:
  jshead:
    - js/jquery-1.9.1.min.js
    - js/highcharts.js
    - js/highcharts-more.js
    - js/exporting.js
  cdn:
    jshead:
      - "http://code.jquery.com/jquery-1.9.1.min.js"
      - "http://code.highcharts.com/highcharts.js"
      - "http://code.highcharts.com/highcharts-more.js"
      - "http://code.highcharts.com/modules/exporting.js"

R code (ReferenceClasses)

R/Highcharts.R

# Class defintion

Highcharts <- setRefClass("Highcharts", contains = "rCharts", methods = list(
    initialize = function() {
      callSuper(); lib <<- 'highcharts'; LIB <<- get_lib(lib)
      params <<- c(params, list(
        ...
      )
    )
  },

  ...

  # Wrapper methods

  title = function(..., replace = T) {
    params$title <<- setSpec(params$title, ..., replace = replace)
  },

  tooltip = function(..., replace = T) {
    params$tooltip <<- setSpec(params$tooltip, ..., replace = replace)
  },

  xAxis = function(..., replace = T) {
    params$xAxis <<- setListSpec(params$xAxis, ..., replace = replace)
  },

  ...

rCharts: Highcharts

a <- Highcharts$new()
a$chart(type = "spline", backgroundColor = NULL)
a$series(data = c(1, 3, 2, 4, 5, 4, 6, 2, 3, 5, NA), dashStyle = "longdash")
a$series(data = c(NA, 4, 1, 3, 4, 2, 9, 1, 2, 3, 1), dashStyle = "shortdot")
a$legend(symbolWidth = 80)
a$set(height = 250)
a

Custom Themes

a$chart(type = "bar", backgroundColor = NULL)
a$xAxis(title = list(enabled = F), type = "category", lineWidth = 0, minorTickLength = 0, tickLength = 0)
a$yAxis(title = list(enabled = F), labels = list(enabled = F), lineWidth = 0, gridLineWidth = 0, minorTickLength = 0, tickLength = 0)
a$legend(enabled = F)

Custom Tooltips

a$tooltip(
  useHTML = T,
  formatter = "#! function() {
    return '<table height=84><tr><td>'
    + '<img src=\"'
    + this.point.url
    + '\" height=80 width=60></td><td>'
    + this.point.text + '<br><br>' + this.point.x + ' years<br>' + this.point.valkrets
    + '</td></tr></table>';} !#"
)

Clickable Points

a$plotOptions(
  scatter = list(
    cursor = "pointer", 
    point = list(
      events = list(
        click = "#! function() { window.open(this.options.url); } !#")), 
    marker = list(
      symbol = "circle", 
      radius = 5
    )
  )
)

Publish on github

a <- create_chart("code.r")
a$publish("My Chart", host = "gist")
# Please enter your github username: reinholdsson
# Please enter your github password: ************
# Your gist has been published
# View chart at http://rcharts.github.io/viewer/?11519927


(not available)

Shiny

server.r: output$chart <- renderChart({ ... }})

ui.r: chartOutput("chart", "highcharts")

(not available)

Highcharts: Shiny Input Binding

server.r

library(shiny)
library(rCharts)
shinyServer(function(input, output) {
  output$text <- renderText({
    sprintf("You have clicked on %s.", input$click$capital)
  })
  output$chart <- renderChart({
    a <- Highcharts$new()
    a$series(data = list(
        list(x = 0, y = 40, capital = "Stockholm"),
        list(x = 1, y = 50, capital = "Copenhagen"),
        list(x = 2, y = 60, capital = "Oslo")
      ), type = "bar")
    a$plotOptions(
      bar = list(
        cursor = "pointer", 
        point = list(
          events = list(

            click = "#! function() { Shiny.onInputChange('click', {capital: this.capital})} !#"

    ))))
    a$addParams(dom = "chart")
    return(a)
  })
})

ui.r

library(shiny)
library(rCharts)
shinyUI(bootstrapPage(chartOutput("chart", "highcharts"), textOutput("text")))

rMaps

... an R package to create, customize and publish interactive maps from R. It supports multiple mapping libraries, including leaflet, datamaps and crosslet.

By: Ramnath Vaidyanathan (aut, cre)

Datamaps

ichoropleth(
  Crime ~ State,
  data = subset(violent_crime, Year == 2010),
  pal = 'PuRd'
)

Animations with AngularJS

ichoropleth(
  Crime ~ State,
  data = violent_crime,
  animate = 'Year'
)

Use argument play = T to add a play button.

Leaflet

# data <- ...
a <- rMaps:::Leaflet$new()
a$setView(c(59.34201, 18.09503), zoom = 13)
a$geoJson(data,
  onEachFeature = '#! function(feature, layer){
    if (feature.properties && feature.properties.popupContent) {
        layer.bindPopup(feature.properties.popupContent);
    }
  } !#',
  pointToLayer =  "#! function(feature, latlng){
    return L.circleMarker(latlng, {
      radius: 6,
      fillColor: feature.properties.fillColor,
      weight: 1,
      fillOpacity: 0.8
    })
  }!#"
)
a$set(height = 300)
a

googleVis

... an R package providing an interface between R and the Google Chart Tools

By: Markus Gesmann (aut, cre), Diego de Castillo (aut), Joe Cheng (ctb)

Sankey

dat <- data.frame(From=c(rep("A",3), rep("B", 3)), 
                  To=c(rep(c("X", "Y", "Z"),2)),
                  Weight=c(5,7,6,2,9,4))
a <- gvisSankey(dat, from="From", to="To", weight="Weight",
                options=list(height=250))

Calendar

a <- gvisCalendar(Cairo, datevar="Date",
                  numvar="Temp",
                  options=list(calendar="{ cellSize: 10 }"))

qtlcharts

By: Karl Broman

ggvis

... implements a interactive grammar of graphics, taking the best parts of ggplot2, combining them with shiny's reactive framework and drawing web graphics using vega.

By: RStudio, Inc.


Thank you!

Use a spacebar or arrow keys to navigate