Skip to content

Instantly share code, notes, and snippets.

@s-u
Forked from smschauhan/part1.md
Created May 15, 2014 22:41
Show Gist options
  • Save s-u/9255f2c4b48225146e9d to your computer and use it in GitHub Desktop.
Save s-u/9255f2c4b48225146e9d to your computer and use it in GitHub Desktop.
rCharts - demo

rCharts in rCloud

This is a demo of using rCharts along with rCloud. It requires installation of rCharts >= 0.4.2 and the latest version of rCloud.

Our first step is to load rCharts and set the appropriate options so that the charts are rendered as inline iframes.

library(rCharts)
options(
  rcharts.mode = 'iframesrc', 
  rcharts.cdn = TRUE,
  RCHART_WIDTH = 700,
  RCHART_HEIGHT = 400
)

In this demo, we will explore the different chart types supported by the visualization library NVD3, which is a wrapper around d3.js, and provides several interactive controls out of the box.

Scatter Chart

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

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

Multibar Horizontal Chart

p3 <- nPlot(~ cyl, group = 'gear', data = mtcars, type = 'multiBarHorizontalChart')
p3$chart(showControls = F)
p3

Pie Chart

p4 <- nPlot(~ cyl, data = mtcars, type = 'pieChart')
p4

Donut Chart

p5 <- nPlot(~ cyl, data = mtcars, type = 'pieChart')
p5$chart(donut = TRUE)
p5

Line Chart

data(economics, package = 'ggplot2')
p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6

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$x2Axis( tickFormat="#!function(d) {
  return d3.time.format('%Y')(new Date( d * 86400000 ));
}!#" )
p7$xAxis( NULL, replace = T)
p7

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
# scratch file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment