This is the first part of the Data Visualization in Analytics series using rCharts. I’ll be testing multiple interactive charts and plots using the rCharts package. This is a very powerful library in R developed and maintained by Ramnath Vaidyanath.

Lets start with the very famous dataset in R, the iris dataset. Just for illustration purpose, the first 10 rows of the datset is loaded.

setwd("C:/Users/rarroyave/Desktop")
data(iris)
library(knitr)
kable(iris[1:6,], digits = 2, align = 'c', caption = "IRIS Dataset")
IRIS Dataset
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

The dataset comprises of 3 kinds of species with 50 observations from each type. For each sample, the sepal and petal length and width were registered.

1. NVD3 Plot

Here i will demostrate the use of d3js library. The NVD3 plot can be used to make interactive graphs.

We will exlore the data using the plotting features in rCharts. There are three kinds of Species in the dataset namely, Sentosa, Versicolor and Virginica. Lets check the features of these three class of species if the width and length of the petals have some disparity.

library(rCharts)
library(dplyr)
library(reshape2)

my_iris <- tbl_df(iris) %>%
          group_by(Species) %>%
          summarize(Petal.Width = mean(Petal.Width),
                    Petal.Length = mean(Petal.Length),
                    Sepal.Length = mean(Sepal.Length),
                    Sepal.Width = mean(Sepal.Width))

melted <- melt(my_iris, id=c("Species"))

Plot1 <- nPlot(value ~ Species, group = "variable", data = melted, type = "multiBarChart")
Plot1$show('iframesrc', cdn = TRUE)
#Plot1$save('chart1.html', cdn=TRUE)
#cat('<iframe src="chart1.html" width=100%, height=420></iframe>') 

The plot that we have created is called a multibarplot. It is quite apparent from the plot the reason it is named as multibarplot. This plot has two options in the display mode.

  • Grouped Mode: In the the grouped mode, the vertical bars are placed juxtaposing each other within the same group.
  • Stacked Mode: In the Stacking mode, the units are placed on yop of each other. It becomes convenient to explan data to non-tech savvy person using plots that are interactive in nature.

2. Leaflet

Leaflet is a opensource JavaScript library for embedding maps into apps. It has a lot of pluggins that helps in extending its features.

Here is a code snippet that can be used to embed maps into apps.

map3 <- Leaflet$new()
map3$setView(c(30.615978, -96.345249), zoom = 15)
map3$marker(c(30.619179, -96.346568), bindPopup = "<p> Rebel Draught House. This is where i party! </p>")
map3$marker(c(30.614956, -96.342380), bindPopup = "<p> This is my university. Whoop! </p>")
map3$show('iframesrc', cdn = TRUE)
#map3$save('chart2.html', cdn=TRUE)
#cat('<iframe src="chart2.html" width=100%, height=450></iframe>') 

Tags or popups can be added to the maps to indicate a location of interest in the map. The package also allows you to change the default popup image to anything you want.


3. Morris Plots

Morris plots can be used to visualize time-series data especially with a large number of observations. It uses the MorrisJS library to create a visually aesthetic plot.

We will use the dataet available in the R dataset package, the EuStockMarkets. This dataset contains the daily closing prices of major European stock indices: Germany DAX (Ibis), Switzerland SMI, France CAC, and UK FTSE. The top 6 rows of the data is shown as a sample.

IRIS Dataset
DAX SMI CAC FTSE Date
1628.75 1678.1 1772.8 2443.6 1991-01-01
1613.63 1688.5 1750.5 2460.2 1991-01-02
1606.51 1678.6 1718.0 2448.2 1991-01-03
1621.04 1684.1 1708.1 2470.4 1991-01-04
1618.16 1686.6 1723.1 2484.7 1991-01-05
1610.61 1671.6 1714.3 2466.8 1991-01-06

We will plot the Date on the x-axis of the plot and the stock market prices of different markets in the y-axis. Its important to note that MorrisJS needs the dates to be in character format. The default date format that R used will not generate the desired plot.

m1 <- mPlot(x = "Date", y = c("DAX", "SMI","CAC","FTSE"), type = "Line", data = ts_data)
m1$set(pointSize = 0, lineWidth = 1)
m1$show('iframesrc', cdn = TRUE)
#m1$save('chart3.html', cdn = TRUE)
#cat('<iframe src="chart3.html" width=100%, height=450></iframe>')

As you roll the mouse pointer over the plot, the plot automatically displays the value of the stock on that particular day.


4. PolyCharts

The last plot in this series in the polychart.

For this graph illustration, we will use the CO2 dataset in R. The CO2 data frame has 84 rows and 5 columns of data from an experiment on the cold tolerance of the grass species Echinochloa crus-galli.

data("CO2")
rplot <- rPlot(conc ~ uptake | Type + Treatment, data = CO2, type = "point", color = "Plant")
rplot$show('iframesrc', cdn = TRUE)
#rplot$save('chart4.html', cdn = TRUE)
#cat('<iframe src="chart4.html" width=100%, height=420></iframe>')

If you click on any datapoint, it pops up additional information. This plot is pretty useful if you are trying to visualize data the more than one factor and numerical variables.

Here are some of the fancy plots you can create in R using the rCharts package with various libraries.

In my next series of Data Visualization in Analytics, i will cover the googlevis package. It is a very simple to use library that creates HTML webpages that inturn calls the Google API to make the ineractive plots. Stay tuned!