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)
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")
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 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")
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)
## mid counts Species
## setosa.1 4.3 4 setosa
## setosa.2 4.5 5 setosa
## setosa.3 4.7 7 setosa
## setosa.4 4.9 12 setosa
## setosa.5 5.1 11 setosa
## setosa.6 5.3 6 setosa
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")
sessionInfo()
## R version 3.0.1 (2013-05-16)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] rCharts_0.3.52 knitr_1.4.1
##
## loaded via a namespace (and not attached):
## [1] digest_0.6.3 evaluate_0.4.7 formatR_0.9 grid_3.0.1
## [5] lattice_0.20-15 plyr_1.8 RJSONIO_1.0-3 stringr_0.6.2
## [9] tools_3.0.1 whisker_0.3-2 yaml_2.1.7