Loading Libraries

library(knitr)
library(printr)
library(rCharts)

Dataset used in this document

mtcars

head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

HairEyeColor

hair_eye = as.data.frame(HairEyeColor)
hair_eye
Hair Eye Sex Freq
Black Brown Male 32
Brown Brown Male 53
Red Brown Male 10
Blond Brown Male 3
Black Blue Male 11
Brown Blue Male 50
Red Blue Male 10
Blond Blue Male 30
Black Hazel Male 10
Brown Hazel Male 25
Red Hazel Male 7
Blond Hazel Male 5
Black Green Male 3
Brown Green Male 15
Red Green Male 7
Blond Green Male 8
Black Brown Female 36
Brown Brown Female 66
Red Brown Female 16
Blond Brown Female 4
Black Blue Female 9
Brown Blue Female 34
Red Blue Female 7
Blond Blue Female 64
Black Hazel Female 5
Brown Hazel Female 29
Red Hazel Female 7
Blond Hazel Female 5
Black Green Female 2
Brown Green Female 14
Red Green Female 7
Blond Green Female 8

economics

library(ggplot2)
head(economics)
date pce pop psavert uempmed unemploy
1967-06-30 507.8 198712 9.8 4.5 2944
1967-07-31 510.9 198911 9.8 4.7 2945
1967-08-31 516.7 199113 9.0 4.6 2958
1967-09-30 513.3 199311 9.8 4.9 3143
1967-10-31 518.5 199498 9.7 4.7 3066
1967-11-30 526.2 199657 9.4 4.8 3018

Plot 1: Scatter Chart

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

p1$xAxis(axisLabel = 'Weight')

p1$show('inline', include_assets = TRUE)

\(\\\)

Plot 2: 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$show('inline', include_assets = TRUE)

\(\\\)

Plot 3: MultiBar Horizontal Chart

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

p3$chart(showControls = F)

p3$show('inline', include_assets = TRUE)

\(\\\)

Plot 4: Pie Chart

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

p4$show('inline', include_assets = TRUE)

\(\\\)

Plot 5: Donut Chart

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

p5$chart(donut = TRUE)

p5$show('inline', include_assets = TRUE)

\(\\\)

Plot 6: Line Chart

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

p6$show('inline', include_assets = TRUE)

\(\\\)

Plot 7: Line with Focus Chart

library(reshape2)

ecm <- melt(economics[,c('date', 'uempmed', 'psavert')], id = 'date')

head(ecm)
date variable value
1967-06-30 uempmed 4.5
1967-07-31 uempmed 4.7
1967-08-31 uempmed 4.6
1967-09-30 uempmed 4.9
1967-10-31 uempmed 4.7
1967-11-30 uempmed 4.8
p7 <- nPlot(value ~ date, group = 'variable', data = ecm, type = 'lineWithFocusChart')

#dates from R to JSON will come over as number of days since 1970-01-01
#so convert to milliseconds 86400000 in a day and then format with d3
#on lineWithFocusChart type xAxis will also set x2Axis unless it is specified
p7$xAxis( tickFormat="#!function(d) 
          {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#" )

p7$show('inline', include_assets = TRUE)

\(\\\)

Plot 8: 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)))

head(dat)
t var val
0 A 26
0 B 32
0 C 12
0 D 35
1 A 38
1 B 28
p8 <- nPlot(val ~ t, 
            group =  'var', 
            data = dat, 
            type = 'stackedAreaChart', 
            id = 'chart')

p8$show('inline', include_assets = TRUE)

\(\\\)

Plot 9: 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$show('inline', include_assets = TRUE)

\(\\\)

Plot 10: InteractiveGuidline(Multi-Tooltips) on Stack

dat <- data.frame(t=rep(0:23,each=4),
                  var=rep(LETTERS[1:4],4),
                  val=round(runif(4*24,0,50)))

p10 <- nPlot(val ~ t, 
            group =  'var', 
            data = dat, 
            type = 'stackedAreaChart', 
            id = 'chart')

p10$chart(useInteractiveGuideline=TRUE)

p10$show('inline', include_assets = TRUE)

\(\\\)

Plot 11: showDistX and showDistY

p11 <- nPlot(mpg ~ wt, 
            group = 'cyl', 
            data = mtcars, 
            type = 'scatterChart')

p11$xAxis(axisLabel = 'Weight')

p11$chart(showDistX = TRUE, showDistY = TRUE)

p11$show('inline', include_assets = TRUE)

\(\\\)

Plot 12: multiChart

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$show('inline', include_assets = TRUE)

\(\\\)

Plot 13: Sparklines

p13 <- nPlot(uempmed ~ date, 
             data = economics, 
             type = 'sparklinePlus',
             height=100,
             width=500)

p13$chart(xTickFormat="#!function(d) {
          return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#")


p13$show('inline', include_assets = TRUE)

Reference