Getting started with rCharts

A zoom on highcharts.js

Introduction

rCharts is an R package written by Ramnath Vaidyanathan that wraps powerful javascript interactive charts libraries. It encompasses highcharts.js , Morris.js , Polychart.js, NVD3 and xCharts from D3js, Rickshaw and even Leaflet.

I'll particularly focus on highcharts.js in this blog because I find it very nice!

Don't hesitate to explore a complete live examples of the tutorial here.


Installing rCharts

You can install rCharts using the following commands:

require(devtools)
install_github('rCharts', 'ramnathv')

you may wait a minute as it does not show download progress

rCharts low-level syntax

You can use wrapper functions to build interactive charts as well as slightly lower level syntaxes to understand details behind charts.

Pie chart:

Let's see how to build simple piechart using highcharts.

Output displays distribution of species in Iris dataset

  • First, you need to instantiate new highcharts object:
    a=Highcharts$new()
  • rCharts needs to know the quantitative variable whose distribution will be plotted and values of this distribution:
    data(iris)
    iris$Species=as.character(iris$Species)
    x=unique(iris$Species)
    y=as.numeric(table(iris$Species))
  • You may add some title
    a$title(text = "Number of Species in Iris dataset")
                  

Finally, specify x-value and y-value and the chart type (in this case a pie chart)

a$data(type='pie',x=x,y=y)
then call a

Multiple lines

Let's see how to build multiple lines using highcharts and Iris dataset.

In this case, multiple lines correspond to sepal length and sepal width variables for each observation.

a = Highcharts$new()
a$title(text = "Comparison of Sepal")
a$xAxis(title=list(text="Observations") )
a$series(name="Sepal width", data= iris$Sepal.width )
a$series(name="Sepal length", data= iris$Sepal.length )

You can see from the above code that text in xAxis can be specified. We have also the series attribute which takes arguments such as name and data.

Scatter plot

Scatter plot is often used to visualize relationship between 2 variables. In this tutorial, we examine correlation between petal and sepal length for each of the 3 plant species in Iris dataset.

Nothing special on code side except that you need to specify the chart type (scatter). Also, you need to repeat 3-times a$data(...) as 3 different scatter plot are involved here.

One can also define customized color in the color argument of a$data(...)

a = Highcharts$new()
a$chart(type="scatter")
a$title(text = "Sepal vs Petal by species")
a$xAxis(title=list(text="Sepal"))
a$yAxis(title=list(text="Petal"))
a$data(name="setosa",color= "rgba(223, 83, 83,.5)", x=iris[iris$Species=='setosa',"Sepal" y=iris[iris$Species=='setosa',"Petal.Length"])
a$data(name='versicolor',color= "rgba(119, 152, 191, .5)", x=iris[iris$Species=="versicolor","Sepal.Length"], y=iris[iris$Species=="versicolor","Petal.Length"])
a$data(name="virginica",x=iris[iris$Species=="virginica","Sepal.Length"], y=iris[iris$Species=="virginica","Petal.Length"])

Scatter plot + fitted values from regression line

  • The first thing you need to do is to prepare data

3 regression tasks are conducted for each species then x-value and fitted values are store withing a list


lm_setosa=lm(Sepal.Length~Petal.Length,data=iris[iris$Species=='setosa',])
lm_versicolor=lm(Sepal.Length~Petal.Length,data=iris[iris$Species=='versicolor',])
lm_virginica=lm(Sepal.Length~Petal.Length,data=iris[iris$Species=='virginica',])
data_setosa=cbind(iris[iris$Species=='setosa',"Petal.Length"],lm_setosa$fit) data_versicolor=cbind(iris[iris$Species=='versicolor',"Petal.Length"],lm_versicolor$fit) data_virginica=cbind(iris[iris$Species=='virginica',"Petal.Length"],lm_virginica$fit) list_setosa=list() for ( i in 1:nrow(data_setosa) ) { list_setosa[[i]]=data_setosa[i,] } list_versicolor=list() for ( i in 1:nrow(data_versicolor) ) { list_versicolor[[i]]=data_versicolor[i,] } list_virginica=list() for ( i in 1:nrow(data_setosa) ) { list_virginica[[i]]=data_virginica[i,] }
  • After all,data are inserted into the chart of type line.
 
    a = Highcharts$new()
a$title(text="Sepal vs Petal by species(+ regression lines)")
a$xAxis(min=min(iris[,input$x[1]]),title=list(text="Petal Length")
a$yAxis(title=list(text="Sepal Length"))
a$series(type='line', name='fitted values setosa',color= 'rgba(223, 83, 83,.5)',data=list_setosa)
#insertion
a$series(type='line', name='fitted values versicolor',color= 'rgba(119, 152, 191, .5)',data=list_versicolor)
a$series(type='line', name='fitted values virginica',data=list_virginica)
a$data(type='scatter',name='setosa',color= 'rgba(223, 83, 83,.5)', x=iris[iris$Species=='setosa',"Petal.Length"], y=iris[iris$Species=='setosa',"Sepal.Length"])
a$data(type='scatter',name='versicolor',color= 'rgba(119, 152, 191, .5)', x=iris[iris$Species=='versicolor',"Petal.Length"], y=iris[iris$Species=='versicolor',"Sepal.Length"])
a$data(type='scatter',name='virginica', x=iris[iris$Species=='virginica',"Petal.Length"], y=iris[iris$Species=='virginica',"Sepal.Length"])

Combo chart

In this example, we will plot an histogram and a line chart on the same graph.

We are going to plot the mean of sepal length by species:


    #data preparation for the mean line:
    x1=mean(iris[iris$Species=='setosa',"Sepal.Length"])
    x2=mean(iris[iris$Species=='versicolor',"Sepal.Length"])
    x3=mean(iris[iris$Species=='virginica',"Sepal.Length"])
    x=cbind(x1,x2,x3)
    # chart:
    a = Highcharts$new()
    a$chart(type = "column") #you can change to "bar"
    a$title(text = "Mean Sepal length by species (+Average)")
    a$xAxis(categories = unique(as.character(iris$Species)))
    a$yAxis(title = list(text = "Sepal.Length"))
    a$data(x,name='Mean')
    a$series(type='spline',name='average'
             ,data= x )
    a