Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
92 lines (74 sloc) 3.45 KB

Markdown example with knitr and rCharts

=========================================== This is an example file showing how to use rCharts with knitr/markdown. rCharts an R package to create, customize and publish interactive javascript visualizations from R.

Let's first install it. It lives on github, not on CRAN yet.

require(devtools)
install_github('rCharts', 'ramnathv', ref = 'dev')

This part is mainly needed to import CSS files that sets up width/height for the plots. It imports CSS files and JavaScript libraries from online resources. This is slightly different from the original version that exists in rCharts. In this version, you can add additional CSS files with the "css" argument. "rNVD3.css" is required for proper plot width/height for plots from the NVD3 library.

## load the package
library(rCharts)

## utility function to add required assets such as CSS and JS libraries
add_lib_assets <- function(lib, cdn = F,css=NULL) {
    assets = get_assets(get_lib(lib), cdn = cdn)
    if(!is.null(css)){assets$css=c(assets$css,css)}
    styles <- lapply(assets$css, function(style) {
        sprintf("<link rel='stylesheet' href=%s>", style)
    })

    scripts <- lapply(assets$jshead, function(script) {
        sprintf("<script type='text/javascript' src=%s></script>", script)
    })
    cat(paste(c(styles, scripts), collapse = "\n"))
}

# get assets from online repositories 
add_lib_assets("NVD3",cdn=TRUE,css="http://rawgithub.com/ramnathv/rCharts/master/inst/libraries/nvd3/css/rNVD3.css") 
add_lib_assets("Polycharts",cdn=TRUE)

Scatter plot using Polychart

The chunk below shows how to produce a simple scatter plot using Polychart library.


names(iris) = gsub("\\.", "", names(iris))
r1<-rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')
r1$print("polyScatter")

Multi barchart using NVD3

hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")
n1$print("nvd3mbar")

Scatter Plot from NVD3

Scatter plot example with rCharts using NVD3 JS library

data(iris)
sepal <- iris[,c(1:2,5)]

n2 <- nPlot(Sepal.Length ~ Sepal.Width, data =sepal, type = "scatterChart",group="Species")
n2$xAxis(axisLabel="Sepal.Width") # add x axis label
n2$yAxis(axisLabel="Sepal.Length")
n2$print("nvd3Scatter")

Histogram Plot from NVD3

Let's try to plot a multihistogram with rCharts using NVD3 library. We need to first calculate break points and mid points for the histogram bars and produce a single data frame that has the counts, mid-points for bars and group information.

data(iris)
sepalw <- iris[,c(1,5)]
hst=hist(sepalw[,1],plot=FALSE,breaks=20)

data=by(sepalw,sepalw$Species,function(x) data.frame(mid=hst$mids,counts=hist(x[,1],breaks=hst$breaks,plot=FALSE)$counts,Species=rep(x[1,2],length(hst$breaks)-1) )  )
data=do.call("rbind",data)
head(data)

We got the data in the right format, now let's plot the histogram with multiBarChart

n3 <- nPlot(counts ~ mid, data=data,type = "multiBarChart",group="Species")
n3$xAxis(axisLabel="Sepal.Width")
n3$yAxis(axisLabel="counts")
n3$chart(color = c('red', 'blue', 'green'))
n3$print("nvd3Hist")

Session info

sessionInfo()