Author: Markus Gesmann and Diego de Castillo
Date: 2024-05-25

# Introduction to googleVis

#### Markus Gesmann and Diego de Castillo

#### 2024-05-25

Abstract

The googleVis package provides an interface between R and the Google Charts API. Google Charts offer interactive charts which can be embedded into web pages. The functions of the googleVis package allow the user to visualise data stored in R data frames with Google Charts without uploading the data to Google. The output of a googleVis function is HTML code that contains the data and references to JavaScript functions hosted by Google. googleVis makes use of the internal R HTTP server to display the output locally. A modern browser with an Internet connection.

# 1 Introduction

## 1.1 Motivation

In 2006 Hans Rosling gave an inspiring talk at TED (Rosling 2006) about social and economic developments in the world over the past 50 years, which challenged the views and perceptions of many listeners. Rosling had used extensive data analysis to reach his conclusions. To visualise his talk, he and his team at Gapminder (Foundation 2010) had developed animated bubble charts, aka motion charts.

Rosling’s presentation popularised the idea and use of interactive charts. One year later the software behind Gapminder was bought by Google and integrated as motion charts into their Google Charts API (Inc. 2012a), formerly known as Google Visualisation API.

In 2010 Sebastián Pérez Saaibi (Saaibi 2010) presented at the R/Rmetrics Workshop on Computational Finance and Financial Engineering, the idea to use Google motion charts to visualise R output with the R.rsp package (Bengtsson 2012).

Inspired by those talks and the desire to use interactive data visualisation tools to foster the dialogue between data analysts and others the authors of this vignette started the development of the googleVis package (Gesmann and Castillo 2011) in August 2010.

### 1.1.1 Google Chart Tools

The Google Charts API (Inc. 2012a) allows users to create interactive charts as part of Google documents, spreadsheets and web pages. In this text, we will focus on the usage of the API as part of web pages.

The Google Public Data Explorer (Inc. 2012c) provides a good example, demonstrating the use of interactive charts and how they can help to analyse data.

The charting data can either be embedded into the HTML file or read dynamically. The key to the Google Charts is that the data is structured in a DataTable (Inc. 2012d), and this is where the googleVis package helps, as it transforms R data frames into JSON (JSON.org 2006) objects, using the jsonlite package (Ooms 2014), as the basis for a DataTable.

As an example we shall look at the html-code of a motion chart from Google’s visualisation gallery (Inc. 2012b).

     1 <html>
     2  <head>
     3   <script type="text/javascript"
     4     src="https://www.google.com/jsapi">
     5   </script>
     6   <script type="text/javascript">
     7     google.load('visualization', '1',
     8      {'packages':['motionchart']});
     9    google.setOnLoadCallback(drawChart);
    10    function drawChart() {
    11    var data=new google.visualization.DataTable();
    12    data.addColumn('string', 'Fruit');
    13    data.addColumn('date', 'Date');
    14    data.addColumn('number', 'Sales');
    15    data.addColumn('number', 'Expenses');
    16    data.addColumn('string', 'Location');
    17    data.addRows([
    18    ['Apples',new Date(1988,0,1),1000,300,'East'],
    19    ['Oranges',new Date(1988,0,1),1150,200,'West'],
    20    ['Bananas',new Date(1988,0,1),300,250,'West'],
    21    ['Apples',new Date(1989,6,1),1200,400,'East'],
    22    ['Oranges',new Date(1989,6,1),750,150,'West'],
    23    ['Bananas',new Date(1989,6,1),788,617,'West']
    24    ]);
    25    var chart=new google.visualization.MotionChart(
    26      document.getElementById('chart_div'));
    27    chart.draw(data, {width: 600, height:300});
    28   }
    29   </script>
    30  </head>
    31  <body>
    32   <div id="chart_div"
    33        style="width:600px; height:300px;">
    34   </div>
    35  </body>
    36 </html>

The code and data are processed and rendered by the browser and is not submitted to any server1.

You will notice that the above HTML code has five generic parts:

- references to Google’s AJAX (l. 4) and Visualisation API (ll. 7 - 8),
- data to visualise as a `DataTable` (ll. 11 - 24),
- an instance call to create the chart (ll. 25 - 26),
- a method call to draw the chart including options, shown here as width and height (l. 27),
- an HTML `<div>` element to add the chart to the page (ll. 32 – 34).

These principles hold true for most of the interactive charts of the Google Chart Tools, see the examples in the next section.

However, before you use the API you should read the Google Terms of Service (Inc 2012).

# 2 The googleVis package

The googleVis package provides an interface between R and the Google Chart Tools. The functions of the package allow the user to visualise data stored in R data frames with Google Charts.

The output of a googleVis function is HTML code that contains the data and references to JavaScript functions hosted by Google. A browser with an Internet connection is required to view the output, and for a very few chart types, notably motion charts, also Flash. Examples of several chart types are shown below, which have been combined with the `gvisMerge` function.

## 2.1 Installation

You can install googleVis in the usual way from CRAN, e.g.:

    install.packages('googleVis')

The installation was successful if the command `library(googleVis)` gives you the following message:

    library(googleVis)

    ##
    ## Welcome to googleVis version 0.7.3
    ##
    ## Please read Google's Terms of Use
    ## before you start using the package:
    ## https://developers.google.com/terms/
    ##
    ## Note, the plot method of googleVis will by default use
    ## the standard browser to display its output.
    ##
    ## See the googleVis package vignettes for more details,
    ## or visit https://mages.github.io/googleVis/.
    ##
    ## To suppress this message use:
    ## suppressPackageStartupMessages(library(googleVis))

# 3 Concepts of the googleVis package

The individual functions of the googleVis package are documented in the help pages. Here we will cover only the basic concepts of the package.

As an example, we will show how to generate a geo chart. It works similarly for the other APIs. Further examples are covered in the demos2 of the googleVis package.

The design of the visualisation functions is fairly generic. The name of the visualisation function is `'gvis' + ChartType`. So for a geo chart we have:

    gchart <-  gvisGeoChart(data,
                         locationvar = "",
                         colorvar = "",
                         sizevar = "",
                         hovervar = "",
                         options = list(),
                         chartid)

Here `data` is the input `data.frame`, `locationvar`, `colorvar`, `sizevar` and `hovervar` specify the various columns used for the plot. Display options are set in an optional list, which we discuss in more detail later. The options and data requirements follow those of the Google Charts API and are documented in the help pages, see

    help('gvisGeoChart')

The argument `chartid` allows the user to set a chart ID of the output chart manually. If the argument is missing a random ID using `tempfile(pattern='')` will be generated. Unique chart IDs are required to place more than one chart on a web page.

The output of a googleVis function is a list of lists (a nested list) containing information about the chart type, chart ID and the HTML code in a sub-list with header, chart, caption and footer.

The idea behind this concept is that users can get a complete web page, while at the same time offer a facility to extract specific parts, such as the chart itself. This is particularly helpful if the package functions are used in solutions where the user wants to feed the visualisation output into other sites.

The output of a googleVis function will be of class `gvis` and `list`. Generic print (`print.gvis`) and plot (`plot.gvis`) functions exist to ease the handling of such objects.

To illustrate the concept we shall create a Geo chart using the `Exports` data set.

## 3.1 Geo Chart Example

Following the documentation of the Google Geo Chart API we need a data set which has at least one column with the location variable.

As an example we use the `Exports` data set:

    data(Exports)
    Exports

    ##          Country Profit Online
    ## 1        Germany      3   TRUE
    ## 2         Brazil      4  FALSE
    ## 3  United States      5   TRUE
    ## 4         France      4   TRUE
    ## 5        Hungary      3  FALSE
    ## 6          India      2   TRUE
    ## 7        Iceland      1  FALSE
    ## 8         Norway      4   TRUE
    ## 9          Spain      5   TRUE
    ## 10        Turkey      1  FALSE

Here we will use the columns `'Country'` and `'Profit'` as location and colour variable respectively.

     gchart <-  gvisGeoChart(data = Exports,
                          locationvar='Country',
                          colorvar='Profit',
                          options=list(projection="kavrayskiy-vii",
                                       width=400, height=200))

The structural output of `gvisGeoChart` is a list of lists as described below.

     str(gchart)

    ## List of 3
    ##  $ type   : chr "GeoChart"
    ##  $ chartid: chr "GeoChartID136f0b1a719e"
    ##  $ html   :List of 4
    ##   ..$ header : chr "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
    ##   ..$ chart  : Named chr [1:7] "<!-- GeoChart generated in R 4.4.0
    ##   .. ..- attr(*, "names")= chr [1:7] "jsHeader" "jsData" "jsDrawCh
    ##   ..$ caption: chr "<div><span>Data: Exports &#8226; Chart ID: <a
    ##   ..$ footer : chr "\n<!-- htmlFooter -->\n<span> \n  R version 4.
    ##  - attr(*, "class")= chr [1:2] "gvis" "list"

The first two items of the list contain information about the chart type used and the individual chart ID:

    gchart$type

    ## [1] "GeoChart"

    gchart$chartid

    ## [1] "GeoChartID136f0b1a719e"

The html output is a list with header, chart, caption and footer. This allows the user to extract only certain parts of the page, or to create a complete html page.

The header part of the html page has only basic html and formatting tags:

    print(gchart, tag='header')

    ## <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    ##   "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    ## <html xmlns="https://www.w3.org/1999/xhtml">
    ## <head>
    ## <title>GeoChartID136f0b1a719e</title>
    ## <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    ## <style type="text/css">
    ## body {
    ##   color: #444444;
    ##   font-family: Arial,Helvetica,sans-serif;
    ##   font-size: 75%;
    ##   }
    ##   a {
    ##   color: #4D87C7;
    ##   text-decoration: none;
    ## }
    ## </style>
    ## </head>
    ## <body>

Here we used the `print` statement with the tag `'header'` instead of `gchart$html$header` to achieve a formatted screen output. This is the same output as `cat(gchart$html$chart)`.

The actual Google visualisation code is stored with the data as a named character vector in the `chart` item of the HTML list. The chart is made up of several JavaScript and HTML statements. Please notice that the JavaScript functions are uniquely named with the information of the chart ID. This concept allows the user get all the chart code directly or only specific parts; see the examples in the help page of `print.gvis` for more details.

    names(gchart$html$chart)

    ## [1] "jsHeader"       "jsData"         "jsDrawChart"    "jsDisplayChart"
    ## [5] "jsFooter"       "jsChart"        "divChart"

The complete chart can be displayed via:

    print(gchart, tag='chart')  ## or cat(gchart$html$chart)

    ## <!-- GeoChart generated in R 4.4.0 by googleVis 0.7.3 package -->
    ## <!-- Sat May 25 15:07:41 2024 -->
    ##
    ##
    ## <!-- jsHeader -->
    ## <script type="text/javascript">
    ##
    ## // jsData
    ## function gvisDataGeoChartID136f0b1a719e () {
    ## var data = new google.visualization.DataTable();
    ## var datajson =
    ## [
    ##  [
    ## "Germany",
    ## 3
    ## ],
    ## [
    ## "Brazil",
    ## 4
    ## ],
    ## [
    ## "United States",
    ## 5
    ## ],
    ## [
    ## "France",
    ## 4
    ## ],
    ## [
    ## "Hungary",
    ## 3
    ## ],
    ## [
    ## "India",
    ## 2
    ## ],
    ## [
    ## "Iceland",
    ## 1
    ## ],
    ## [
    ## "Norway",
    ## 4
    ## ],
    ## [
    ## "Spain",
    ## 5
    ## ],
    ## [
    ## "Turkey",
    ## 1
    ## ]
    ## ];
    ## data.addColumn('string','Country');
    ## data.addColumn('number','Profit');
    ## data.addRows(datajson);
    ## return(data);
    ## }
    ##
    ## // jsDrawChart
    ## function drawChartGeoChartID136f0b1a719e() {
    ## var data = gvisDataGeoChartID136f0b1a719e();
    ## var options = {};
    ## options["width"] = 400;
    ## options["height"] = 200;
    ## options["projection"] = "kavrayskiy-vii";
    ##
    ##
    ##     var chart = new google.visualization.GeoChart(
    ##     document.getElementById('GeoChartID136f0b1a719e')
    ##     );
    ##     chart.draw(data,options);
    ##
    ##
    ## }
    ##
    ##
    ## // jsDisplayChart
    ## (function() {
    ## var pkgs = window.__gvisPackages = window.__gvisPackages || [];
    ## var callbacks = window.__gvisCallbacks = window.__gvisCallbacks || [];
    ## var chartid = "geochart";
    ##
    ## // Manually see if chartid is in pkgs (not all browsers support Array.indexOf)
    ## var i, newPackage = true;
    ## for (i = 0; newPackage && i < pkgs.length; i++) {
    ## if (pkgs[i] === chartid)
    ## newPackage = false;
    ## }
    ## if (newPackage)
    ##   pkgs.push(chartid);
    ##
    ## // Add the drawChart function to the global list of callbacks
    ## callbacks.push(drawChartGeoChartID136f0b1a719e);
    ## })();
    ## function displayChartGeoChartID136f0b1a719e() {
    ##   var pkgs = window.__gvisPackages = window.__gvisPackages || [];
    ##   var callbacks = window.__gvisCallbacks = window.__gvisCallbacks || [];
    ##   window.clearTimeout(window.__gvisLoad);
    ##   // The timeout is set to 100 because otherwise the container div we are
    ##   // targeting might not be part of the document yet
    ##   window.__gvisLoad = setTimeout(function() {
    ##   var pkgCount = pkgs.length;
    ##   google.load("visualization", "1", { packages:pkgs, callback: function() {
    ##   if (pkgCount != pkgs.length) {
    ##   // Race condition where another setTimeout call snuck in after us; if
    ##   // that call added a package, we must not shift its callback
    ##   return;
    ## }
    ## while (callbacks.length > 0)
    ## callbacks.shift()();
    ## } });
    ## }, 100);
    ## }
    ##
    ## // jsFooter
    ## </script>
    ##
    ## <!-- jsChart -->
    ## <script type="text/javascript" src="https://www.google.com/jsapi?callback=displayChartGeoChartID136f0b1a719e"></script>
    ##
    ## <!-- divChart -->
    ##
    ## <div id="GeoChartID136f0b1a719e"
    ##   style="width: 400; height: 200;">
    ## </div>

Similarly you can also access specific components of the chart, e.g. (output truncated)

    cat(gchart$html$chart['jsChart']) # or print(gchart, 'jsChart')

    ##
    ## <!-- jsChart -->
    ## <script type="text/javascript" src="https://www.google.com/jsapi?c

A basic chart caption and html footer are the final items of the html list (output truncated):

    print(gchart, tag='caption')

    ## <div><span>Data: Exports &#8226; Chart ID: <a href="Chart_GeoChart

    print(gchart, tag='footer')

    ##
    ## <!-- htmlFooter -->
    ## <span>
    ##   R version 4.4.0 (2024-04-24)
    ##   &#8226; <a href="https://developers.google.com/terms/">Google Te
    ## </span></div>
    ## </body>
    ## </html>

# 4 Displaying gvis objects locally

To display the page locally, simply type:

    plot(gchart)  # returns invisibly the file name

The plot method for `gvis`-objects creates HTML files in a temporary folder using the type and chart ID information of the object and it will display the output using the R built-in web server.

Note that the chart caption provides a link to the chart code via the chart ID for copy and paste.

The R command `tempdir()` will show you the path of the per-session temporary directory, in which the files were written. You can write the chart into a local html file via the `print` command with the file argument, e.g.

    print(gchart, file="myGoogleVisChart.html")

Alternatively use the function `plot.gvis` explicitly, e.g. suppose your html file is stored in `/Users/JoeBloggs/myGoogleVisChart.html`. Using the `plot.gvis` the file will be copied into a temporary directory and displayed via the R HTTP help server with, in the same way as a gvis-object:

    plot.gvis("/Users/JoeBloggs/myGoogleVisChart.html")

# 5 Setting options

Setting the various options of a googleVis objects can be a bit cumbersome at first. The options follow those of the Google Charts API and can be set via a named list using the argument `options`. In the following example, we create a line chart and set various options:

    df <- data.frame(country=c("US", "GB", "BR"),
                     val1=c(1,3,4), val2=c(23,12,32))

    Line <-  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
                           ))
    plot(Line)

As you can see from the example above, the simpler options can be set by `name=value`, e.g. `width=500`, while the more complex options with sub-components are listed in curly brackets `{}` and arrays `[]`, e.g. to define the two axes.

Generally, the following rules apply:

- parameters with names that do not include a “.” are set with a single value, e.g. width and height. Those are set like one would do in R, that is `options=list(width=200, height=300)`. Boolean arguments are set to either `TRUE` or `FALSE`, using the R syntax.
- parameters with names that do not include a “.” and are set with multiple values, e.g. `color`, and are wrapped in `[ ]`, e.g. `options=list(colors="['#cbb69d', '#603913', '#c69c6e']")`
- parameters with names that do include a “.” present parameters with several sub-options and have to be set as a string wrapped in “{ }”. The values of those sub-options are set via `parameter:value`. Boolean values have to be stated as `'true'` or `'false'`. For example the Google documentation states the formatting options for the vertical axis as `vAxis.format`. Then this parameter can be set in R as: `options=list(vAxis="{format:'#,###%'}")`.
- If several sub-options have to be set, e.g. `titleTextStyle.color`, `titleTextStyle.fontName` and `titleTextStyle.fontSize`, then those can be combined in one list item such as: `options=list(titleTextStyle="{color:'red',fontName:'Courier', fontSize:16}")`
- parameters that can have more than one value per sub-options are wrapped in `[ ]`. For example to set the labels for left and right axes use: `options=list(vAxes="[{title:'val1'}, {title:'val2'}]")`

## 5.1 Chart Editor

A special option for all charts is `gvis.editor`, which adds an edit button to the page, allowing the user to edit, change and customise the chart on the fly. The content of the option `gvis.editor` describes the label of the browser button.

    Editor <- gvisLineChart(df, options=list(gvis.editor='Edit me!'))
    plot(Editor)

# 6 Embedding googleVis in web sites

Suppose you have an existing web page and would like to integrate the output of a googleVis function, such as `gvisLineChart`. In this case you only need the chart output from `gvisLineChart`. So you can either copy and paste the output from the R console

    print(gchart, 'chart')  ## or cat(gchart$html$chart)

into your existing html page, or write the content directly into a file

    print(gchart, 'chart', file='myfilename')

and process it from there.

# 7 Using googleVis with shiny

Shiny is a package by RStudio, which makes it incredibly easy to build interactive web applications with R.

With version 0.4.0 of googleVis support for shiny apps was added. Joe Cheng contributed the `renderGvis` function which allows users to use googleVis output in shiny in a similar way to other plotting functions.

The following example has been taken from the help file of `renderGvis`. It displays a scatter chart where the user can select the data set to be displayed.

    # server.R
    library(googleVis)

    shinyServer(function(input, output) {
      datasetInput <- reactive({
        switch(input$dataset,
               "rock" = rock,
               "pressure" = pressure,
               "cars" = cars)
      })

      output$view <- renderGvis({
        gvisScatterChart(datasetInput())
      })
    })

    # ui.R
    shinyUI(pageWithSidebar(
      headerPanel("googleVis on Shiny"),
      sidebarPanel(
        selectInput("dataset", "Choose a dataset:",
                    choices = c("rock", "pressure", "cars"))
      ),
      mainPanel(
        htmlOutput("view")
      )
    ))

You can run the example locally with the following statement.

    library(shiny)
    runApp(system.file("shiny/", package="googleVis"))

# 8 Setting default behaviour of `print.gvis` and `plot.gvis`

In googleVis version 0.3.2 the function `plot.gvis` gained the same argument as `print.gvis`: `tag`. By default the `tag` argument is set to `NULL` in `plot.gvis` and the plot function will display its output in a browser window. However, if `tag` is not `NULL` the function `plot.gvis` will behave exactly like `print.gvis`.

The default `tag` can be set for both functions globally via the `options()` function. On package load googleVis sets `options(gvis.print.tag='html')` and `options(gvis.plot.tag=NULL)`.

Suppose you would set `options(gvis.plot.tag='chart')` then all following plot statements would print the chart part of the gvis-object only, without opening a browser window. This might seem a bit odd at first, yet it becomes helpful when you write R Markdown files for `knitr` or files for other packages such as `R.rsp`.

While you draft your file you may want to see the output of googleVis in an interactive way, so you set `options(gvis.plot.tag=NULL)` at the top of the file and you change the setting to `'chart'` before you parse the file, say with `knitr`. This will ensure that all plot statements return the HTML code of the chart, rather than opening browser windows.

# 9 Using googleVis with knitr

Using googleVis with knitr (Xie 2013) is a convenient way of creating interactive reproducible reports. The approach taken by knitr is similar to Sweave, you can combine R code with text and formatting tags. However, knitr can also export to HTML, which is required to embed googleVis charts.

To include googleVis output into a knitr document you have to set the `self_contained` option to `false` in the YAML header:

    ---
    title: "My document"
    output:
      html_document:
        self_contained: false
    ---

Furthermore, the chunk option `results` should be set to `'asis'`, so that the html output is written into the markdown file.

You can either use the `print` statement:

    ```{r results='asis'}
    gchart <- gvisColumnChart(CityPopularity, 'City', 'Popularity',
                            options=list(width=550, height=450,
                                         legend='none'))
    print(gchart, 'chart')
    ```

or alternative change the behaviour of the `plot` function via setting `options(gvis.plot.tag = 'chart')`. With this setting `plot(x)` will no longer open a browser window, but produce the same output as `print(x, tag='chart')`, if `x` is a gvis-object.

Hence, setting the option `gvis.plot.tag` in a knitr markdown Rmd-file to `'chart'` will automatically turn all following plot statements into html output.

Note that you can use the `options()` command in your knitr file to switch between an interactive mode, where you are likely to experiment, via copying and pasting R code into the console and running `knit` on the whole file.

A more comprehensive example is given in the help file to `?plot.gvis`.

# 10 Using googleVis in presentations

The Google Chart Tools are designed for web pages, so it should be no surprise that it can be difficult or impossible to embed googleVis output in traditional presentation software like MS PowerPoint, Google Docs, OpenOffice Impress or Apple Keynote.

The easiest way is to include screen shots into the slide with links to the live web pages. But this approach requires the presenter to switch between applications during her talk. This can be fun, but quite often it is not.

An alternative would be to build the presentation as a web page itself.

A popular approach here is the `slidify` package by Ramnath Vaidyanathan, (Vaidyanathan 2012) that builds on the knitr Markdown approach of the previous section. An example of a `slidify` presentation is the googleVis tutorial given at the useR! conference in 2013, Gesmann and Castillo (2013).

# 11 Using Trendlines with googleVis

A trendline is a line superimposed on a chart revealing the overall direction of the data. Google Charts can automatically generate trendlines for Scatter Charts, Bar Charts, Column Charts and Line Charts.

For more details visit: https://developers.google.com/chart/interactive/docs/gallery/trendlines

## 11.1 Linear trend line

    plot(
      gvisScatterChart(women, options=list(trendlines="0"))
    )

## 11.2 Exponential trend line with equation shown in legend

    plot(
      gvisScatterChart(women, options=list(
        trendlines="{0: { type: 'exponential',
                         visibleInLegend: 'true',
                         color: 'green',
                         lineWidth: 10,
                         opacity: 0.5}}",
        chartArea="{left:50,top:20,width:'50%',height:'75%'}"))
    )

## 11.3 Add trend line to column chart

    dat <- data.frame(val1=c(1,3,4,5,6,8),
                      val2=c(12,23,32,40,50,55))
    plot(
      gvisColumnChart(dat,
                      options=list(trendlines="{0: {}}"))
    )

## 11.4 Changing labels in legend

    dat$val3 <- c(5,6,10,12,15,20)
    plot(
      gvisColumnChart(dat,
                      options=list(trendlines="{
                              0: {
                                labelInLegend: 'Trendline 1',
                                visibleInLegend: true,},
                              1:{
                                labelInLegend: 'Trendline 2',
                                visibleInLegend: true}
                              }",
                              chartArea="{left:50,top:20,
                                          width:'50%',height:'75%'}"
                      ))
    )

# 12 Using Roles via googleVis

Roles add the ability to pass columns for further processing downstream. Role columns must follow column they pertain to. Proper naming conventions must be observed. For example, roles fulfilling tooltip roles and must be called “foo.blah.tooltip”. For more details see the Google documentation.

The following examples should help to illustrate the concept.

The first example uses a data set that has the additional column `pop.html.tooltip` with the first 11 letters of the Latin alphabet. This column is mapped automatically as a tooltip when the user hovers over the chart point.

    df <- data.frame(year=1:11,pop=1:11,
                     pop.html.tooltip=letters[1:11])
    plot(
      gvisScatterChart(df)
    )

HTML code can be embedded into the tooltip as well, if the option `isHtml` is set to true.

Often it is helpful to highlight certain parts of the data. The Google API distinguishes between certainty and emphasis. In a similar way to above additional columns with boolean values have to be added to the data.

    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))
    )

Using roles with column or bar charts has some specifics. Instead of ‘emphasize’ use ‘style’ to change the colours.

    dat <- data.frame(Year=2010:2013,
                      Sales=c(600, 1500, 800, 1000),
                      Sales.html.tooltip=c('$600K in our first year!',
                                           'Sunspot activity made this our best year ever!',
                                           '$800K in 2012.',
                                           '$1M in sales last year.'),
                      Sales.certainty=c(TRUE, FALSE, TRUE, FALSE))
    plot(
      gvisColumnChart(dat, xvar='Year',
                      yvar=c('Sales', 'Sales.certainty')
      )
    )

    df <- data.frame(country=c("US", "GB", "BR"),
                     val1=c(1,3,4),
                     val1.emphasis=c(TRUE, TRUE, FALSE),
                     val2=c(23,12,32))
    plot(
      gvisLineChart(df, xvar="country",
                    yvar=c("val1", "val1.emphasis")
      )
    )

    plot(
      gvisLineChart(df, xvar="country",
                    yvar=c("val1", "val1.emphasis", "val2")
      )
    )

Setting the annotations style to ‘line’ allows adding little reference lines to the plot.

    dat <- data.frame(Year=2010:2013,
                      Sales=c(600, 1500, 800, 1000),
                      Sales.annotation=c('First year', NA, NA, 'Last Year'),
                      Sales.annotationText=c('$600K in our first year!',
                                           NA,
                                           NA,
                                           '$1M in sales last year.'))

    plot(
      gvisLineChart(dat, xvar='Year',
                      yvar=c('Sales',
                             'Sales.annotation',
                             'Sales.annotationText'),
                             options=list(annotations = "{style:'line'}")
      )
    )

Intervals help to add error bars, confidence levels, etc. Note that the options are set either via `interval` or `intervals`, if set to all intervals. The examples below give an indication of what can be achieved with intervals. For more details visit the Google documentation.

    df <- data.frame(Year=2013:2014, Sales=c(120, 130),
                     Sales.interval.1=c(100,110),
                     Sales.interval.2=c(140, 150),
                     Sales.interval.3=c(90, 100),
                     Sales.interval.4=c(150, 170),
                     Sales.style=c('red', 'gold'),
                     Sales.annotation=c("North", "South"),
                     check.names=FALSE)

    plot(
      gvisBarChart(df, xvar='Year',
                   yvar=c('Sales',
                          'Sales.interval.1',
                          'Sales.interval.2',
                          'Sales.style',
                          'Sales.annotation')
      )
    )

    plot(
      gvisLineChart(df, xvar='Year',
                   yvar=c('Sales',
                          'Sales.interval.1',
                          'Sales.interval.2'),
                   options=list(series="[{color:'purple'}]")
      )
    )

    plot(
      gvisLineChart(df, xvar='Year',
                    yvar=c('Sales',
                           'Sales.interval.1',
                           'Sales.interval.2',
                           'Sales.interval.3',
                           'Sales.interval.4'),
                    options=list(series="[{color:'purple'}]",
                                 lineWidth=4,
                                 interval="{
                'i1': { 'style':'line', 'color':'#D3362D', 'lineWidth': 0.5 },
                'i2': { 'style':'line', 'color':'#F1CA3A', 'lineWidth': 1 },
                'i3': { 'style':'line', 'color':'#5F9654', 'lineWidth': 2 },
                'i4': { 'style':'line', 'color':'#5F9654', 'lineWidth': 3 }
            }")
      )
    )

    plot(
      gvisLineChart(df, xvar='Year',
                    yvar=c('Sales',
                           'Sales.interval.1',
                           'Sales.interval.2',
                           'Sales.interval.3',
                           'Sales.interval.4'),
                    options=list(series="[{color:'purple'}]",
                                 lineWidth=4,
                                 intervals="{ 'style':'area' }",
                                 interval="{
                                   'i1': { 'color': '#4374E0', 'style':'bars', 'barWidth':0, 'lineWidth':4, 'pointSize':10, 'fillOpacity':1 },
                                   'i2': { 'color': '#E49307', 'style':'bars', 'barWidth':0, 'lineWidth':4, 'pointSize':10, 'fillOpacity':1 }}"
                                 )
      )
    )

The tool tips in geo charts need a little work around, a `Tooltip.header` variable has to be set to an empty string:

    Exports$Profit.tooltip <- paste("<b>Test</b>", Exports$Profit)
    Exports$Tooltip.header <- ""
    GeoTooltip <- gvisGeoChart(
      Exports,
      locationvar = "Country",
      colorvar = "Profit",
      hovervar = "Tooltip.header",
      options = list(tooltip = "{isHtml: true}")
    )
    plot(GeoTooltip)

# 13 Beyond R

In this section we present ideas which go beyond the usual coding in R and are somewhat experimental.

## 13.1 Registering to catch events

Google visualisations can fire and receive events. It exposes the following two JavaScript methods:

- `google.visualization.events.trigger()` fires an event,
- `google.visualization.events.addListener()` listens for events.

Here is an example of registering to receive the selection event from the Google documentation:

    var table = new google.visualization.Table(document.getElementById('table_div'));
    table.draw(data, options);
    google.visualization.events.addListener(table, 'select', selectHandler);

    function selectHandler() {
      alert('A table row was selected');
    }

We will only deal with this special case of a ‘select’ event of the ‘addListner’ method. This event is available for most visualisations and acts on user interactions, e.g. user selection clicks.

The ‘addListener’ method expects JavaScript code, which can be embedded into a gvis-object via `options` as (undocumented) parameter `gvis.listener.jscode`.

Here are some examples:

Look up the selected item in Wikipedia:

    jscode <- "window.open('https://en.wikipedia.org/wiki/'
                      + data.getValue(chart.getSelection()[0].row,0)); "

    J1 <- gvisGeoChart(Exports, locationvar='Country', sizevar = 'Profit',
                     options=list(dataMode="regions",
                                  gvis.listener.jscode=jscode))
    plot(J1)

In the same way we can use the code in other charts, e.g. org- or line charts:

    plot(gvisOrgChart(Regions,  options=list(gvis.listener.jscode=jscode)))
    plot(gvisLineChart(Regions[,c(1,3)], options=list(gvis.listener.jscode=jscode)))

In the following more advanced example the selected value of a table is displayed in a message box:

    jscode <- "
           var sel = chart.getSelection();
           var row = sel[0].row;
           var text = data.getValue(row,1);
           alert(text);
    "
    J2 <- gvisTable(Population[1:5,],
                    options=list(gvis.listener.jscode=jscode))
    plot(J2)

For more details see the `demo(EventListener)` and Google Charts Reference.

# 14 Frequent ask questions – FAQ

## 14.1 Is there an alternative for the Flash based motion chart

Other R packages provide alternatives to the Flash based Google motion chart, such as plotly, gganimate

## 14.2 Can I use googleVis output in PDF files?

No, not directly. The Google Charts API is designed for dynamic web output on your screen and not on paper.

For further details see Google’s online documentation on printing PNG charts.

## 14.3 Can I change the colour of the bubbles in motion charts?

No, unfortunately not. The colours are set by the Google Charts API and cannot be changed by the user.

## 14.4 Why can’t I see motion charts when I open them from a local directory?

Note that Flash charts may not work when loaded as a local file due to security settings, and therefore require to be displayed via a web server. However, you can overcome this issue by changing your Flash security settings. Tony Breyal posted the following solution on stackoverflow.com:

1. Go to https://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html
2. Click on the dropbox which says ’Edit location’ and choose ’add location’
3. Click ’browse for folder’
4. Choose the folder in which you saved your HTML file
5. Click OK

## 14.5 Why can’t I see motion charts on my iPad/iPhone?

Motion charts (also geo maps and annotated time lines) are rendered in your browser using Flash, unlike most other charts which use HTML5. Unfortunately, Flash is not directly supported on iOS devices such as iPads and iPhones.

## 14.6 How can I set axes limits with googleVis?

Unfortunately, there are no arguments such as `ylim` and `xlim`. Instead, the Google Charts axes options are set via `hAxes` and `vAxes`, with ‘h’ and ‘v’ indicating the horizontal and vertical axis. More precisely, we have to set `viewWindowMode:'explicit'` and set the `viewWindow` to the desired `min` and `max` values. Additionally, we have to wrap all of this in `[{}]` brackets as those settings are sub options of `h/vAxes`. There are also options `minValue` and `maxValue`, but they only allow you to extend the axes ranges.

Here is a minimal example, setting the y-axis limits from 0 to 10:

    dat <- data.frame(x=LETTERS[1:10],
                      y=c(0, 4, -2, 2, 4, 3, 8, 15, 10, 4))
    area1 <- gvisAreaChart(xvar="x", yvar="y", data=dat,
            options=list(vAxes="[{viewWindowMode:'explicit',
                viewWindow:{min:0, max:10}}]",
                            width=500, height=400,
                            title="y-limits set from 0 to 10"),
                chartid="area1ylim")
    plot(area1)

## 14.7 How do I deal with apostrophes in column names?

The googleVis package converts data frames into JSON objects. The column names of the resulting JSON tables are encapsulated with single speech marks.

Hence apostrophes in column names of your input data frame have to be encapsulated by a double backslash.

Here is a little example:

    df <- data.frame("Year"=c('2009', '2010'),
                     "Alice\\'s salary"=c(86.1, 93.3),
                     "Bob\\'s salary"=c(95.3, 100.5),
                     check.names=FALSE)
    gchart <- gvisColumnChart(df, options=list(vAxis='{baseline:0}',
                                title="Salary",
                                legend="{position:'bottom'}"))
    plot(gchart)

## 14.8 How can I change the look and feel of the charts?

The charts have a lot of options which allow you to change the look and feel of the output, see the help files for more details. However, googleVis provides only an interface to the Google Charts API. If you have specific questions to the charts then please ask the Google Visualisation API newsgroup.

For frequent ask questions regarding the API check: https://developers.google.com/chart/interactive/faq.

## 14.9 Is it possible to use googleVis in corporate work?

Review the Google Terms of Service and get in touch with your colleagues in IT / Legal. If in doubt contact Google directly.

# 15 Bugs and issues

Should you find any issues or bugs with googleVis, then please drop us a line or add them to our issues list: (https://github.com/mages/googleVis/issues)

# 16 Citation

Please cite googleVis if you use it in your work or publications:

    citation("googleVis")

    ## To cite googleVis in publications, please use
    ##
    ##   Markus Gesmann and Diego de Castillo. Using the Google Visualisation
    ##   API with R. The R Journal, 3(2):40-44, December 2011.
    ##
    ## A BibTeX entry for LaTeX users is
    ##
    ##   @Article{,
    ##     title = {googleVis: Interface between R and the Google Visualisation API},
    ##     author = {Markus Gesmann and Diego {de Castillo}},
    ##     journal = {The R Journal},
    ##     year = {2011},
    ##     volume = {3},
    ##     number = {2},
    ##     pages = {40--44},
    ##     month = {December},
    ##     url = {https://journal.r-project.org/archive/2011-2/RJournal_2011-2_Gesmann+de~Castillo.pdf},
    ##   }

# 17 References

Bengtsson, Henrik. 2012. “R.rsp: R Server Pages.” https://CRAN.R-project.org/package=R.rsp.
Foundation, Gapminder. 2010. “Gapminder.” https://www.gapminder.org.
Gesmann, Markus, and Diego de Castillo. 2011. “Using the Google Visualisation API with R.” The R Journal 3 (2): 40–44. https://journal.r-project.org/archive/2011-2/RJournal_2011-2_Gesmann+de~Castillo.pdf.
———. 2013. googleVis Tutorial. useR! 2013, University of Castilla-La Mancha, Albacete, Spain.
Inc, Google. 2012. “Google API Terms of Service.” https://developers.google.com/terms/.
Inc., Google. 2012a. “Google Chart Tools.” https://developers.google.com/chart/interactive/docs/gallery.
———. 2012b. “Google Motion Chart API.” https://developers.google.com/chart/interactive/docs/gallery/motionchart.html.
———. 2012c. “Google Public Data Explorer.” https://www.google.com/publicdata/directory.
———. 2012d. “Google Visualisation Reference.” https://developers.google.com/chart/interactive/docs/reference.
JSON.org. 2006. “JSON.” https://www.json.org/.
Ooms, Jeroen. 2014. “The Jsonlite Package: A Practical and Consistent Mapping Between JSON Data and r Objects.” arXiv:1403.2805. https://arxiv.org/abs/1403.2805.
Rosling, Hans. 2006. “TED Talk: Hans Rosling Shows the Best Stats You’ve Ever Seen.” TED Talk; https://www.ted.com/talks/hans_rosling_the_best_stats_you_ve_ever_seen.
Saaibi, Sebastián Pérez. 2010. R/RMETRICS Generator Tool for Google Motion Charts. https://www.rmetrics.org/.
Vaidyanathan, Ramnath. 2012. Slidify: Generate Reproducible Html5 Slides from r Markdown. https://github.com/ramnathv/slidify/.
Xie, Yihui. 2013. Knitr: A General-Purpose Package for Dynamic Report Generation in r. https://CRAN.R-project.org/package=knitr.

1. Data Policy↩︎

2. See `demo(package="googleVis")` for a list of the available demos↩︎