R Interface to Google Charts

The googleVis package provides an interface between R and the Google’s charts tools. It allows users to create web pages with interactive charts based on R data frames. Charts are displayed locally via the R HTTP help server. A modern browser with Internet connection is required and for some charts a Flash player. The data remains local and is not uploaded to Google.

You find examples of all googleVis function on CRAN. Perhaps the best known example is the motion chart, popularised by Hans Rosling in his 2006 TED talk.

Please read Google’s Terms of Use before you start using the package.

Installation

You can install the stable version from CRAN:

install.packages('googleVis')

To install the current development version from github you need the devtools package and the other packages on which googleVis depends:

install.packages(c("devtools","jsonlite", "knitr", "shiny", "httpuv"))

To install googleVis run:

library(devtools)
install_github("mages/googleVis")

Charts in googleVis

Line Chart

Set the googleVis options first to change the behaviour of plot.gvis, so that only the chart component of the HTML file is written into the output file.

suppressPackageStartupMessages(library(googleVis))
## Creating a generic function for 'toJSON' from package 'jsonlite' in package 'googleVis'
op <- options(gvis.plot.tag='chart')
df=data.frame(country=c("US", "GB", "BR"), 
              val1=c(10,13,14), 
              val2=c(23,12,32))
Line<-gvisLineChart(df)
plot(Line)

Bar Chart

Bar<-gvisBarChart(df)
plot(Bar)

Scatter chart

Scatter <- gvisScatterChart(women, 
                            options=list(
                              legend="none",
                              lineWidth=2, pointSize=0,
                              title="Women", vAxis="{title:'weight (lbs)'}",
                              hAxis="{title:'height (in)'}", 
                              width=300, height=300))
plot(Scatter)

Add edit button for on the fly customisation

Line4 <-  gvisLineChart(df, "country", c("val1","val2"),
                        options=list(gvis.editor="Edit me!"))
plot(Line4)

Cusomizing Plots

Line3 <-  gvisLineChart(df, xvar="country", yvar=c("val1","val2"),
                        options=list(
                          title="Hello World",
                          titleTextStyle="{color:'red', 
                                           fontName:'Courier', 
                                           fontSize:16}",                         
                          backgroundColor="#D3D3D3",                          
                          vAxis="{gridlines:{color:'red', count:3}}",
                          hAxis="{title:'Country', titleTextStyle:{color:'blue'}}",
                          series="[{color:'green', targetAxisIndex: 0}, 
                                   {color: 'orange',targetAxisIndex:1}]",
                          vAxes="[{title:'val1'}, {title:'val2'}]",
                          legend="bottom",
                          curveType="function",
                          width=500,
                          height=300                         
                        ))

Boxplot

Candle <- gvisCandlestickChart(OpenClose, 
                               options=list(legend='none'))
plot(Candle)

Pie chart

Pie <- gvisPieChart(CityPopularity)
plot(Pie)

A map

Geo=gvisGeoChart(Exports, locationvar="Country", 
                 colorvar="Profit",
                 options=list(projection="kavrayskiy-vii"))
plot(Geo)

Tables

Table <- gvisTable(Stock, 
                   formats=list(Value="#,###"))
plot(Table)

Merging charts

G <- gvisGeoChart(Exports, "Country", "Profit", 
                  options=list(width=300, height=300))
T <- gvisTable(Exports, 
               options=list(width=220, height=300))

GT <- gvisMerge(G,T, horizontal=TRUE) 
plot(GT)

Things you can do with googleVis

  • The visualizations can be embedded in websites with HTML code
  • Dynamic visualizations can be built with Shiny, Rook, and R.rsp
  • Embed them in R markdown based documents
  • Set results="asis" in the chunk options
  • Can be used with knitr and slidify

For more info