This is a demo of rCharts. Install rCharts using devtools::install_github('ramnathv/rCharts').

Before we begin charting, let us customize some settings for rCharts and knitr

library(rCharts)
library(plyr)
library(knitr)
library(reshape2)
library(scales)
options(RCHART_WIDTH = 600, RCHART_HEIGHT = 400)
knitr::opts_chunk$set(comment = NA, results = 'asis', tidy = F, message = F)

Time to get charting now!


Polychart

We will create our first chart using Polychart, a javascript charting library based on the grammar of graphics, and inspired by ggplot2.

Facetted Scatterplot

names(iris) = gsub("\\.", "", names(iris))
p1 = rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')
p1$print(include_assets=T)

Facetted Barplot

hair_eye = as.data.frame(HairEyeColor)
p2 = rPlot(Freq ~ Hair, color = 'Eye', data = hair_eye, type = 'bar')
p2$facet(var = 'Eye', type = 'wrap', rows = 2)
p2$print(include_assets=T)

Boxplot

data(tips, package = 'reshape2')
p3 = rPlot(x = 'day', y = 'box(tip)', data = tips, type = 'box')
p3$print(include_assets=T)

Column Chart

dat = count(mtcars, .(gear, am))
p4 = rPlot(x = 'bin(gear, 1)', y = 'freq', data = dat, type = 'bar', 
  list(var = 'am', type = 'wrap'))
p4$print(include_assets=T)

Heat Map

dat = expand.grid(x = 1:5, y = 1:5)
dat = transform(dat, value = rnorm(25))
p5 = rPlot(x = 'bin(x, 1)', y = 'bin(y, 1)', color = 'value', data = dat, type = 'tile')
p5$print(include_assets=T)

NBA Heat Map

nba = read.csv('http://datasets.flowingdata.com/ppg2008.csv')
nba.m = ddply(melt(nba), .(variable), transform, rescale = rescale(value))
p6 = rPlot(Name ~ variable, color = 'rescale', data = nba.m, type = 'tile', height = 600)
p6$guides("{color: {scale: {type: gradient, lower: white, upper: steelblue}}}")
p6$print(include_assets=T)

Another Facetted Scatter Chart

r1 = rPlot(mpg ~ wt | am + vs, data = mtcars, type = 'point',
  color = 'gear')
r1$print(include_assets=T)

Morris

The next library we will be exploring is MorrisJS.

Multi Line chart

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

Simple Bar Chart

haireye = as.data.frame(HairEyeColor)
dat = subset(haireye, Sex == "Female" & Eye == "Blue")
p1 = mPlot(x = 'Hair', y = list('Freq'), data = dat, type = 'Bar', labels = list("Count"))
p1$set(hideHover = "auto")
p1$print(include_assets=T)

Multi Bar Chart

dat = subset(haireye, Sex == "Female")
p2 = mPlot(Freq ~ Eye, group = "Hair", data = dat, type = "Bar", labels = 1:4)
p2$print(include_assets=T)

Area Chart

data(economics, package = 'ggplot2')
dat = transform(economics, date = as.character(date))
p3 <- mPlot(x = "date", y = list("psavert", "uempmed"), data = dat, type = 'Line',
 pointSize = 0, lineWidth = 1)
p3$set(xLabelFormat = "#! function (x) { 
  return x.toString(); } 
!#")
p3$set(type = 'Area')
p3$print(include_assets=T)

NVD3

NVD3 is d3js library, which produces amazing interactive visualizations with little customization.

Scatter Chart

p1 = nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart')
p1$xAxis(axisLabel = 'Weight')
p1$print(include_assets=T)

MultiBar Chart

hair_eye = as.data.frame(HairEyeColor)
p2 = nPlot(Freq ~ Hair, group = 'Eye', data = subset(hair_eye, Sex == "Female"), type = 'multiBarChart')
p2$chart(color = c('brown', 'blue', '#594c26', 'green'))
p2$print(include_assets=T)

MultiBar Horizontal Chart

p3 = nPlot(~ cyl, group = 'gear', data = mtcars, type = 'multiBarHorizontalChart')
p3$chart(showControls = F)
p3$print(include_assets=T)

Pie Chart

p4 = nPlot(~ cyl, data = mtcars, type = 'pieChart')
p4$print(include_assets=T)

Donut Chart

p5 = nPlot(~ cyl, data = mtcars, type = 'pieChart')
p5$chart(donut = TRUE)
p5$print(include_assets=T)

Line Chart

data(economics, package = 'ggplot2')
p6 = nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6$print(include_assets=T)

Line with Focus Chart

ecm = reshape2::melt(economics[,c('date', 'uempmed', 'psavert')], id = 'date')
p7 = nPlot(value ~ date, group = 'variable', data = ecm, type = 'lineWithFocusChart')
p7$xAxis( tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#" )
p7$print(include_assets=T)

Stacked Area Chart

dat = data.frame(t=rep(0:23,each=4),var=rep(LETTERS[1:4],4),val=round(runif(4*24,0,50)))
p8 = nPlot(val ~ t, group =  'var', data = dat, type = 'stackedAreaChart', id = 'chart')
p8$print(include_assets=T)

InteractiveGuidline(Multi-Tooltips) on Line

p9 = nPlot(value ~ date, group = 'variable', data = ecm, type = 'lineChart')
p9$xAxis( tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#" )
#try new interactive guidelines feature
p9$chart(useInteractiveGuideline=TRUE)
p9$print(include_assets=T)

InteractiveGuidline(Multi-Tooltips) on Stack

p10 = nPlot(val ~ t, group =  'var', data = dat, type = 'stackedAreaChart', id = 'chart')
p10$chart(useInteractiveGuideline=TRUE)
p10$print(include_assets=T)

showDistX and showDistY

p11 = nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart')
p11$xAxis(axisLabel = 'Weight')
p11$chart(showDistX = TRUE, showDistY = TRUE)
p11$print(include_assets=T)

Multi Chart

p12 = nPlot(value ~ date, group = 'variable', data = ecm, type = 'multiChart')
p12$params$multi = list(
  uempmed = list(type="area",yAxis=1),
  psavert = list(type="line",yAxis=2)
)
p12$setTemplate(script = system.file("/libraries/nvd3/layouts/multiChart.html",package = "rCharts"))
p12$print(include_assets=T)

Sparklines

p16 = nPlot(uempmed ~ date, data = economics, type = 'sparklinePlus',height=100,width=500)
p16$chart(xTickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#")
p16$print(include_assets=T)

xCharts

Line Chart

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

Bar Chart

haireye = subset(as.data.frame(HairEyeColor), Sex == "Male")
p2 <- xCharts$new()
p2$set(xScale = 'ordinal', yScale = 'linear', width = 600)
p2$layer(Freq ~ Hair, group = 'Eye', data = haireye, type = 'bar')
p2$print(include_assets=T)

HighCharts

Scatter Chart

h1 = hPlot(Pulse ~ Height, data = MASS::survey, type = "scatter", group = "Exer")
h1$print(include_assets=T)

Bubble Chart

h2 = hPlot(Pulse ~ Height, data = MASS::survey, type = "bubble", title = "Zoom demo", subtitle = "bubble chart", size = "Age", group = "Exer")
h2$chart(zoomType = "xy")
h2$exporting(enabled = F)
h2$print(include_assets=T)

Pie Chart

x = data.frame(key = c("a", "b", "c"), value = c(1, 2, 3))
h3 = hPlot(x = "key", y = "value", data = x, type = "pie")
h3$print(include_assets=T)

Another Scatter Chart

h4 = hPlot(Pulse ~ Height, data = MASS::survey, type = 'scatter', group = 'Sex', radius = 6, group.na = "Not Available")
h4$colors('rgba(223, 83, 83, .5)', 'rgba(119, 152, 191, .5)', 'rgba(60, 179, 113, .5)')
h4$legend(align = 'right', verticalAlign = 'top', layout = 'vertical')
h4$plotOptions(scatter = list(marker = list(symbol = 'circle')))
h4$tooltip(formatter = "#! function() { return this.x + ', ' + this.y; } !#")
h4$print(include_assets=T)

Multi Chart

h5 = hPlot(freq ~ Exer, data = plyr::count(MASS::survey, c('Sex', 'Exer')), type = c('column', 'line'), group = 'Sex', radius = 6)
h5$print(include_assets=T)
h6 = hPlot(x = "Wr.Hnd", y = "NW.Hnd", data = MASS::survey, 
  type = c("line", "bubble", "scatter"), group = "Clap", size = "Age")
h6$print(include_assets=T)

Bar Chart

a = hPlot(freq ~ Exer, data = plyr::count(MASS::survey, c('Sex', 'Exer')), type = 'bar', group = 'Sex', group.na = 'NA\'s')
a$plotOptions(bar = list(cursor = 'pointer', point = list(events = list(click = "#! function() { alert ('Category: '+ this.category +', value: '+ this.y); } !#"))))
a$print(include_assets=T)

Column Chart

df = data.frame(
    name = c("John", "John", "Joe", "Joe", "Jane", "Jane", "Janet", "Janet"),
    data = sample(1:10, 8, replace = T),
    stack = c(rep("male", 4), rep("female", 4))
)
a = rCharts:::Highcharts$new()
a$chart(type = "column")
a$plotOptions(column = list(stacking = "normal"))
a$xAxis(categories = c("Apples", "Oranges"))
invisible(sapply(unique(df$name), function(name){
    d = df[df$name == name, ]
    a$series(name = name, data = d$data, stack = d$stack[[1]])
}))
a$print(include_assets=T)

Leaflet

map3 = Leaflet$new()
map3$setView(c(41.7627, -72.6743), zoom = 13)
map3$marker(c(41.7627, -72.6743), bindPopup = "<p> Hi. I am a popup </p>")
map3$marker(c(41.70, -72.60), bindPopup = "<p> Hi. I am another popup </p>")
map3$print(include_assets=T)

References