-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrCharts.Rmd
91 lines (74 loc) · 3.45 KB
/
rCharts.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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.
```{r,eval=FALSE}
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.
```{r setup, results='asis'}
## 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.
```{r, results='asis'}
names(iris) = gsub("\\.", "", names(iris))
r1<-rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')
r1$print("polyScatter")
```
## Multi barchart using NVD3
```{r nvd3mbar, results='asis',comment=NA}
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
```{r nvd3Scatter, results='asis',comment=NA}
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.
```{r nvd3Hist1}
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**
```{r nvd3Scatter2, results='asis',comment=NA}
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
-------------------------
```{r }
sessionInfo()
```