-
Notifications
You must be signed in to change notification settings - Fork 18
/
rChartsLocalLib.Rmd
68 lines (54 loc) · 2.58 KB
/
rChartsLocalLib.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
# 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')
```
## Scatter plot using Polycharts
```{r, results='asis',out.width=400}
require(rCharts)
names(iris) = gsub("\\.", "", names(iris))
add_lib_assets("Polycharts",cdn=TRUE)
r1<-rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')
r1$print("polyScatter")
```
## Multi barchart using NVD3
```{r nvd3mbar, results='asis',comment=NA}
add_lib_assets("NVD3") # get assets from online repositories
hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")
n1$chart(height=200) # change plot height Chrome/FF doesn't automatically get plot height but safari does
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$chart(height=400) # change plot height Chrome/FF doesn't automatically get plot height but safari does
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'))
n2$chart(height=400) # change plot height Chrome/FF doesn't automatically get plot height but safari does
n3$print("nvd3Hist")
```