• Stars
    star
    1,189
  • Rank 37,458 (Top 0.8 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 11 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Interactive JS Charts from R

rCharts

rCharts is an R package to create, customize and publish interactive javascript visualizations from R using a familiar lattice style plotting interface.

Installation

You can install rCharts from github using the devtools package

require(devtools)
install_github('ramnathv/rCharts')

Features

The design philosophy behind rCharts is to make the process of creating, customizing and sharing interactive visualizations easy.

Create

rCharts uses a formula interface to specify plots, just like the lattice package. Here are a few examples you can try out in your R console.

require(rCharts)

## Example 1 Facetted Scatterplot
names(iris) = gsub("\\.", "", names(iris))
rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')

## Example 2 Facetted Barplot
hair_eye = as.data.frame(HairEyeColor)
rPlot(Freq ~ Hair | Eye, color = 'Eye', data = hair_eye, type = 'bar')

Customize

rCharts supports multiple javascript charting libraries, each with its own strengths. Each of these libraries has multiple customization options, most of which are supported within rCharts. More documentation is underway on how to use rCharts with each of these libraries.

We will create our first chart using Polychart, a javascript charting library based on the grammar of graphics, and inspired by ggplot2.

r1 <- rPlot(mpg ~ wt | am + vs, data = mtcars, type = 'point', color = 'gear')
r1

polychart

There, we have our first embedded chart with nice tooltips! Let me add some interactivity to this chart now using javascript.

graph_chart1.addHandler(function(type, e){
  var data = e.evtData;
  if (type === 'click'){
    return alert("You clicked on car with mpg: " + data.mpg.in[0]);
  }
})

The next library we will be exploring is Morris.

data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
  data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1

morris

Hurray! There we have our second chart!


Next, I will demonstrate my all time favorite d3js library, NVD3, which produces amazing interactive visualizations with little customization.

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

nvd3

See the interactivity that comes at zero cost!


The next library to demo would be xCharts, a slick looking charting library using d3js, made by TenXer.

require(reshape2)
uspexp <- melt(USPersonalExpenditure)
names(uspexp)[1:2] = c('category', 'year')
x1 <- xPlot(value ~ year, group = 'category', data = uspexp, 
  type = 'line-dotted')
x1

xchart

There is your xChart


h1 <- Highcharts$new()
h1$chart(type = "spline")
h1$series(data = c(1, 3, 2, 4, 5, 4, 6, 2, 3, 5, NA), dashStyle = "longdash")
h1$series(data = c(NA, 4, 1, 3, 4, 2, 9, 1, 2, 3, 4), dashStyle = "shortdot")
h1$legend(symbolWidth = 80)
h1

highcharts


map3 <- Leaflet$new()
map3$setView(c(51.505, -0.09), zoom = 13)
map3$marker(c(51.5, -0.09), bindPopup = "<p> Hi. I am a popup </p>")
map3$marker(c(51.495, -0.083), bindPopup = "<p> Hi. I am another popup </p>")
map3

leaflet


usp = reshape2::melt(USPersonalExpenditure)
p4 <- Rickshaw$new()
p4$layer(value ~ Var2, group = 'Var1', data = usp, type = 'area')
p4

rickshaw

Share

rCharts allows you to share your visualization in multiple ways, as a standalone page, embedded in a shiny application, or in a tutorial/blog post.

Standalone

You can publish your visualization as a standalone html page using the publish method. Here is an example. Currently, you can publish your chart as a gist or to rpubs.

## 
names(iris) = gsub("\\.", "", names(iris))
r1 <- rPlot(SepalLength ~ SepalWidth | Species, data = iris, 
  color = 'Species', type = 'point')
r1$publish('Scatterplot', host = 'gist')
r1$publish('Scatterplot', host = 'rpubs')

Shiny Application

rCharts is easy to embed into a Shiny application using the utility functions renderChart and showOutput. Here is an example of an rCharts Shiny App.

## server.r
require(rCharts)
shinyServer(function(input, output) {
  output$myChart <- renderChart({
    names(iris) = gsub("\\.", "", names(iris))
    p1 <- rPlot(input$x, input$y, data = iris, color = "Species", 
      facet = "Species", type = 'point')
    p1$addParams(dom = 'myChart')
    return(p1)
  })
})

## ui.R
require(rCharts)
shinyUI(pageWithSidebar(
  headerPanel("rCharts: Interactive Charts from R using polychart.js"),
  
  sidebarPanel(
    selectInput(inputId = "x",
     label = "Choose X",
     choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'),
     selected = "SepalLength"),
    selectInput(inputId = "y",
      label = "Choose Y",
      choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'),
      selected = "SepalWidth")
  ),
  mainPanel(
    showOutput("myChart", "polycharts")
  )
))

Blog Post

rCharts can also be embedded into an Rmd document using knit2html or in a blog post using slidify. Here are a few examples of tutorials written using rCharts and slidify.

  1. Parallel Coordinate Plots
  2. NY Times Graphics Tutorial

More in the rCharts website and the Gallery.

More

Credits

Most of the implementation in rCharts is inspired by rHighcharts and rVega. I have reused some code from these packages verbatim, and would like to acknowledge the efforts of its author Thomas Reinholdsson.

License

rCharts is licensed under the MIT License. However, the JavaScript charting libraries that are included with this package are licensed under their own terms. All of them are free for non-commercial and commercial use, with the exception of Polychart and Highcharts, both of which require paid licenses for commercial use. For more details on the licensing terms, you can consult the License.md file in each of the charting libraries.

See Also

There has been a lot of interest recently in creating packages that allow R users to make use of Javascript charting libraries.

More Repositories

1

slidify

Generate reproducible html5 slides from R markdown
R
848
star
2

htmlwidgets

HTML Widgets for R
R
775
star
3

rMaps

Interactive Maps from R
JavaScript
390
star
4

rNotebook

A browser based R Notebook
JavaScript
124
star
5

slidifyExamples

Sample Presentations
JavaScript
62
star
6

vuer

Harness the Power of Vue.js in R
R
41
star
7

bikeshare

Visualizing Bike Sharing Networks with rCharts and Shiny
CSS
39
star
8

rNVD3

Interactive Charts from R using NVD3.js
JavaScript
36
star
9

poirot

Reproducible Blogging with R Markdown
R
32
star
10

rChartsCalmap

An htmlwidgets binding for calendar heat maps using d3
JavaScript
31
star
11

rblocks

A fun and visual way to learn data structures and control flow in R.
R
26
star
12

rChartsCalendar

rCharts binding for CalMap
CSS
23
star
13

rChartsShiny

Interactive Visualizations with rCharts and Shiny
CSS
21
star
14

rbokeh

R Bindings to Bokeh
R
18
star
15

dashifyr

A dashboarding framework for R using Shiny
JavaScript
18
star
16

robservable

R package that brings observables to htmlwidgets, allowing for shiny-like interactivity in the browser
16
star
17

slidifyLibraries

This R package contains all external libraries required by slidify
JavaScript
15
star
18

rcstatebin

Interactive Statebin Charts
JavaScript
12
star
19

slickCarousel

jQuery Slick Carousel HTML Widget for R
JavaScript
11
star
20

RGoogleForms

Generate Google Forms from RMarkdown
JavaScript
10
star
21

pycon2014-r

CSS
10
star
22

corise-r-for-ds

CoRise | R for Data Science
Jupyter Notebook
10
star
23

agebyname

R Package to Explore Age vs. Name Relationships
R
9
star
24

slidify-old

Generate reproducible html5 slides from R markdown. This repository has moved to `ramnathv/slidify`
JavaScript
8
star
25

user2014-idocs

Interactive Documents with R: Tutorial at UseR2014
JavaScript
8
star
26

user2014-idocs-slides

Slides for Interactive Documents Tutorial at UseR 2014
JavaScript
6
star
27

sochi

Sochi Olympics Medal Tally using OpenCPU and rCharts
JavaScript
6
star
28

BARUG_Talk

HTML
6
star
29

slidifySite

JavaScript
4
star
30

sachin100s

Timeline of Sachin Tendulkar's 100 Centuries
JavaScript
3
star
31

justGage

justGage HTML Widget for R
JavaScript
3
star
32

NYCBikeShare

Visualizing NYC Bike Share with rCharts and Leaflet
R
3
star
33

ramnathv.ruhoh.com

JavaScript
3
star
34

dbt-explore

Jupyter Notebook
3
star
35

sphinx-test

CSS
3
star
36

agebyname_py

IPython Notebook to Explore Age vs. Name Relationships
3
star
37

Chicago-Mayoral-Elections

R
3
star
38

creating-dashboards-with-shiny-live-training

Dockerfile
3
star
39

carouselDemo

A short demo of how to use bootstrap carousels with Slidify
JavaScript
3
star
40

rstudio_logs

Visualizing RStudio Logs
2
star
41

skewedpositive

My Blog! WIP
HTML
2
star
42

gh3

R Functions to Github API V3
R
2
star
43

styleR

R
2
star
44

binder-shiny-5

Dockerfile
2
star
45

sphinx-test2

Python
2
star
46

learn-github-actions

A sandbox to learn and explore github actions
2
star
47

interactive2

Interactive Documents with R
JavaScript
2
star
48

topojson

CSS
2
star
49

mgcr271

Business Statistics
2
star
50

intellidate

Intelligent parsing of date-time strings
R
2
star
51

r-workshop-mcgill-2021-shiny

R
1
star
52

RExRepos

R Examples Repository
R
1
star
53

rCharts-rbison

rbison map with rCharts + Leaflet
JavaScript
1
star
54

knitr-pandoc

1
star
55

test-wiki

1
star
56

test-course-r1

R
1
star
57

swc-nw-dataviz

Data Visualization Notes, SWC NW 2014
JavaScript
1
star
58

me.jcsims

Code for http://jcsims.me
JavaScript
1
star
59

template-live-webinar-sql

Jupyter Notebook
1
star
60

rcisotype

Interactive Isotype charts using D3js
1
star
61

courses-test-h2o-py

Shell
1
star
62

test-codespaces-jupyter

Jupyter Notebook
1
star
63

r-workshop-mcgill-2021-interactive-plots

R
1
star
64

rvlib

An R package with useful utilities
R
1
star
65

rChartsExtra

Custom interactive charts that make use of rCharts
1
star
66

user2014-rcharts

Interactive Slides on rCharts. Presented at User 2014
JavaScript
1
star
67

poirotBlog

JavaScript
1
star
68

test-r-codespaces

Shell
1
star
69

ggson

Declarative way to produce plots using ggplot2
R
1
star
70

shinySlidify

Shiny apps in a Slidify Deck
JavaScript
1
star
71

testrepo

1
star