googleVis overview & developments

Markus Gesmann
KoelnR, 23 May 2014

Agenda

  • A brief introduction to googleVis
  • Recent developments
  • How I created this presentation with RStudio
  • Summary

Motivation

  • Inspired by Hans Rosling's talk and Gapminder we also wanted interactive data visualisation tools
  • The software behind Hans' talk was bought by Google and integrated as motion charts into their Visualisation API
  • Hence, we had to create googleVis, an R package and interface to the Google Chart Tools API
    • Development started in 2010

Introduction to Google Chart Tools

  • Google Chart Tools provide a way to visualize data on web sites
  • The API makes it easy to create interactive charts
  • It uses JavaScript and DataTable / JSON as input
  • Output is either HTML5/SVG or Flash
  • Browser with internet connection required to display chart
  • Please read the Google Terms of Service before you start

Structure of Google Charts

The chart code has five generic parts:

  1. References to Google's AJAX and Visualisation API
  2. Data to visualize as a DataTable
  3. Instance call to create the chart
  4. Method call to draw the chart including options
  5. HTML <div> element to add the chart to the page

Overview of googleVis

  • The functions of the package allow users to visualize data with the Google Chart Tools without uploading their data to Google
  • The output of googleVis functions is html code that contains the data and references to JavaScript functions hosted by Google
  • To view the output a browser with an internet connection is required, the actual chart is rendered in the browser; some charts require Flash

googleVis version 0.5.2 provides interfaces to

  • Flash based
    • Motion Charts, Annotated Time Lines, Geo Maps
  • HMTL5/SVG based
    • Maps, Geo Charts and Intensity Maps,
    • Tables, Gauges, Tree Maps, Timelines,
    • Line-, Bar-, Column-, Area- and Combo Charts,
    • Scatter-, Bubble-, Candlestick-, Pie- and Org Charts,
    • Annotation-, Calendar-, Histogram and Sankey Charts
  • Support for roles, trendlines and Shiny via renderGvis

Motion chart example

plot(gvisMotionChart(Fruits, "Fruit", "Year"))

The googleVis concept

  • Charts: 'gvis' + ChartType
  • For a motion chart we have
M <- gvisMotionChart(data, 
                     idvar='id', 
                     timevar='date', 
                     options=list(), 
                     chartid)
  • Display the chart by simply plotting the output: plot(M)
  • Plot will generate a temporary html-file and open it in a new browser window

gvis-Chart structure

gvis object structure

List structure:

  • Output of googleVis is a list of list
  • Specific parts can be extracted, e.g.
    • the chart: M$html$chart or
    • data: M$html$chart["jsData"]

Example: Scatter chart

df <- data.frame(year=1:11, x=1:11)
plot(gvisScatterChart(df, options=list(width=600, height=400)))

Example: Line chart with options

df <- data.frame(label=c("US", "GB", "BR"), val1=c(1,3,4), val2=c(23,12,32))
Line <- gvisLineChart(df, xvar="label", yvar=c("val1","val2"),
        options=list(title="Hello World", legend="bottom",
                titleTextStyle="{color:'red', fontSize:18}",                         
                vAxis="{gridlines:{color:'red', count:3}}",
                hAxis="{title:'My Label', 
                        titleTextStyle:{color:'blue'}}",
                series="[{color:'green', targetAxisIndex: 0}, 
                         {color: 'blue',targetAxisIndex:1}]",
                vAxes="[{title:'Value 1 (%)', format:'##,######%'}, 
                                  {title:'Value 2 (\U00A3)'}]",                          
                curveType="function", width=500, height=300                         
                ))

Options in googleVis have to follow the Google Chart API options

Line chart with options

plot(Line)

Example: Displaying geographical information

Plot countries' S&P credit rating sourced from Wikipedia

library(XML)
url <- "http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating"
x <- readHTMLTable(readLines(url), which=3)
levels(x$Rating) <- substring(levels(x$Rating), 4, 
                            nchar(levels(x$Rating)))
x$Ranking <- x$Rating
levels(x$Ranking) <- nlevels(x$Rating):1
x$Ranking <- as.character(x$Ranking)
x$Rating <- paste(x$Country, x$Rating, sep=": ")
G <- gvisGeoChart(x, "Country", "Ranking", hovervar="Rating",
                options=
                  list(gvis.editor="S&P",
                       projection="kavrayskiy-vii",
                       colorAxis="{colors:['#91BFDB', '#FC8D59']}"))

Chart countries' S&P credit rating

plot(G)

New developments since googleVis 0.5.0

  • New functions gvisSankey, gvisAnnotationChart, gvisHistogram, gvisCalendar and gvisTimeline
  • New demos and vigenttes for trendlines and roles
  • New vignettes written in R Markdown
  • Dynamic help pages with roxygen2
  • Moved code development from Google Code with SVN to GitHub with git

New: Add additional columns to define roles

df <- data.frame(year=1:11, x=1:11,
                 x.scope=c(rep(TRUE, 8), rep(FALSE, 3)),
                 y=11:1, y.html.tooltip=LETTERS[11:1],                 
                 y.certainty=c(rep(TRUE, 5), rep(FALSE, 6)),
                 y.emphasis=c(rep(FALSE, 4), rep(TRUE, 7)))
plot(gvisScatterChart(df,options=list(lineWidth=2, width=600, height=400)))

New: Individual markers for Maps

df <- data.frame(Adress=c("EC3M 7HA", "東京都新宿区北新宿2-21-1"),
                 Tip=c("Lloyd's", "Tokyo R User Group"))
plot(gvisMap(df, "Adress", "Tip",
              options=list(mapType='normal',
                icons="{'default': {'normal': 'http://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/Map-Marker-Ball-Azure-icon.png',
                                               'selected': 'http://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/Map-Marker-Ball-Right-Azure-icon.png'}
                                  }", width=800, height=250)))

New: Timeline

dat <- data.frame(Room=c("Room 1","Room 2","Room 3"),
                  Language=c("Japanese", "English", "German"),
                  start=as.POSIXct(c("2014-04-17 14:00", 
                                     "2014-04-17 15:00",
                                     "2014-04-17 14:30")),
                  end=as.POSIXct(c("2014-04-17 15:00", 
                                   "2014-04-17 16:00",
                                   "2014-04-17 15:30")))
plot(gvisTimeline(data=dat, rowlabel="Room", barlabel="Language",
                  start="start", end="end", options=list(width=800, height=250)))

New: Sankey chart

dat <- data.frame(From=c(rep("A",3), rep("B", 3)), 
                  To=c(rep(c("X", "Y", "Z"),2)),
                  Weight=c(5,7,6,2,9,4))
plot(gvisSankey(dat, from="From", to="To", weight="Weight",
                options=list(height=250)))

New: Calendar chart

plot(gvisCalendar(Cairo, datevar="Date",
                  numvar="Temp",
                  options=list(calendar="{ cellSize: 10 }"))

New: Annotation Chart

xtable(tail(Stock, 3))
Date Device Value Title Annotation
10 13882.00 Pens 14334.00 Out of stock Ran out of stock of pens at 4pm
11 13883.00 Pens 66467.00
12 13884.00 Pens 39463.00
A <- gvisAnnotationChart(
  Stock, datevar="Date",
  numvar="Value", idvar="Device",
  titlevar="Title", 
  annotationvar="Annotation",
  options=list(displayAnnotations=TRUE,
               legendPosition='newRow',
               fill=10, displayExactValues=TRUE,
               colors="['#0000ff','#00ff00']",
               width=600, height=300)
  )

New: Annotation Chart

plot(A, tag='chart')

Further reading and examples

How I created this presentation with RStudio

  • Simply started a new presentation file in RStudio and wrote markdown
  • Customised the CSS file
  • Ensured googleVis output is included as html, e.g.
    {r setOptions, message=FALSE}
    library(googleVis)
    options(gvis.plot.tag="chart")
    
  • Used results='asis' to display googleVis plot statements
    {r scatterchart, results="asis"}
    plot(gvisScatterChart(women))
    

Summary

  • googleVis provides an interface to the Google Chart Tools
    • Arguments and parameters follow the Google API
    • Note, Flash charts are only displayed when hosted on a web server, or the local directory has been added as a trusted folder in the Adobe settings
  • Alternative R packages are in development for interactive charts, e.g. rCharts, ggvis

The End. Questions?

Markus Gesmann and Diego de Castillo. Using the Google Visualisation API with R, The R Journal, 3(2):40-44, December 2011 and googleVis package vignette

R markdown code of this presentation is available on GitHub

Session Info

sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] XML_3.98-1.1    xtable_1.7-3    googleVis_0.5.2 knitr_1.5      

loaded via a namespace (and not attached):
[1] evaluate_0.5.5  formatR_0.10    RJSONIO_1.2-0.2 stringr_0.6.2  
[5] tools_3.1.0