- I am an autodidact
- What I present here works for me
- Read and follow the official Google Chart API documentation and Terms of Service
- Sometimes you have re-load this presentation for the charts and all slides to appear
Markus Gesmann
Maintainer of the googleVis and ChainLadder packages
Inspired by Hans Rosling’s talks we wanted to use interactive data visualisation tools to foster the dialogue between data analysts and others
We wanted moving bubbles charts as well
The software behind Hans’ talk was bought by Google and integrated as motion charts into their Visualisation API
Ideally we wanted to use R, a language we knew
Hence, we had to create an interface between the Google Chart Tools and R
googleVis is a package for R and provides an interface between R and the Google Chart Tools
The functions of the package allow users to visualise 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
See also: Using the Google Visualisation API with R, The R Journal, 3(2):40-44, December 2011 and googleVis package vignette
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
The chart code has five generic parts:
library(googleVis)
plot(gvisMotionChart(Fruits, "Fruit", "Year", options = list(width = 600, height = 400)))
Run demo(googleVis)
to see examples of all charts and read the vignette for more details.
library(RJSONIO)
dat <- data.frame(x = LETTERS[1:2], y = 1:2)
cat(toJSON(dat))
## {
## "x": [ "A", "B" ],
## "y": [ 1, 2 ]
## }
M <- gvisMotionChart(data, idvar='id', timevar='date',
options=list(), chartid)
Output of googleVis is a list of list
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
Specific parts can be extracted, e.g.
M$html$chart
or M$html$chart["jsData"]
List structure:
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
plot(Line)
You can enable the chart editor which allows users to change the chart.
plot(gvisLineChart(df, options = list(gvis.editor = "Edit me!", height = 350)))
plot(gvisMotionChart(Fruits, "Fruit", "Year", options = list(width = 500, height = 350)))
Change displaying settings via the browser, then copy the state string from the 'Advanced' tab and set to state
argument in options
.
Ensure you have newlines at the beginning and end of the string.
myStateSettings <- '\n{"iconType":"LINE", "dimensions":{
"iconDimensions":["dim0"]},"xAxisOption":"_TIME",
"orderedByX":false,"orderedByY":false,"yZoomedDataMax":100}\n'
plot(gvisMotionChart(Fruits, "Fruit", "Year",
options=list(state=myStateSettings, height=320)))
Plot countries' S&P credit rating sourced from Wikipedia (requires googleVis 0.4.3)
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",
colorAxis="{colors:['#91BFDB', '#FC8D59']}"))
plot(G)
Display earth quake information of last 30 days
library(XML)
eq <- read.csv("http://earthquake.usgs.gov/earthquakes/feed/v0.1/summary/2.5_week.csv")
eq$loc=paste(eq$Latitude, eq$Longitude, sep=":")
G <- gvisGeoChart(eq, "loc", "Depth", "Magnitude",
options=list(displayMode="Markers",
colorAxis="{colors:['purple', 'red', 'orange', 'grey']}",
backgroundColor="lightblue"), chartid="EQ")
plot(G)
Org <- gvisOrgChart(Regions, options=list(width=600, height=250,
size='large', allowCollapse=TRUE))
plot(Org)
Regions
## Region Parent Val Fac
## 1 Global <NA> 10 2
## 2 America Global 2 4
## 3 Europe Global 99 11
## 4 Asia Global 10 8
## 5 France Europe 71 2
## 6 Sweden Europe 89 3
## 7 Germany Europe 58 10
## 8 Mexico America 2 9
## 9 USA America 38 11
## 10 China Asia 5 1
## 11 Japan Asia 48 11
Notice the data structure. Each row in the data table describes one node. Each node (except the root node) has one or more parent nodes.
Same data structure as for org charts required.
Tree <- gvisTreeMap(Regions, idvar = "Region", parentvar = "Parent", sizevar = "Val",
colorvar = "Fac", options = list(width = 450, height = 320))
plot(Tree)
Display time series data with notes.
head(Stock)
## Date Device Value Title Annotation
## 1 2008-01-01 Pencils 3000 <NA> <NA>
## 2 2008-01-02 Pencils 14045 <NA> <NA>
## 3 2008-01-03 Pencils 5502 <NA> <NA>
## 4 2008-01-04 Pencils 75284 <NA> <NA>
## 5 2008-01-05 Pencils 41476 Bought pencils Bought 200k pencils
## 6 2008-01-06 Pencils 333222 <NA> <NA>
A1 <- gvisAnnotatedTimeLine(Stock, datevar = "Date", numvar = "Value", idvar = "Device",
titlevar = "Title", annotationvar = "Annotation", options = list(displayAnnotations = TRUE,
legendPosition = "newRow", width = 600, height = 300), chartid = "ATLC")
plot(A1)
G <- gvisGeoChart(Exports, "Country", "Profit",
options=list(width=250, height=120))
B <- gvisBarChart(Exports[,1:2], yvar="Profit", xvar="Country",
options=list(width=250, height=260, legend='none'))
M <- gvisMotionChart(Fruits, "Fruit", "Year",
options=list(width=400, height=380))
GBM <- gvisMerge(gvisMerge(G,B, horizontal=FALSE),
M, horizontal=TRUE, tableOptions="cellspacing=5")
plot(GBM)
Suppose you have an existing web page and would like to integrate the output of a googleVis function, such as gvisMotionChart
.
In this case you only need the chart output from gvisMotionChart
. So you can either copy and paste the output from the R console
print(M, "chart") #### or cat(M$html$chart)
into your existing html page, or write the content directly into a file
print(M, "chart", file = "myfilename")
and process it from there.
<iframe width=620 height=300 frameborder="0"
src="http://dl.dropbox.com/u/7586336/RSS2012/line.html">
Your browser does not support iframe
</iframe>
With version 0.3.2 of googleVis plot.gvis
gained the argument 'tag'
, which works similar to the argument of the same name in print.gvis
.
By default the tag argument is NULL
and plot.gvis
has the same behaviour as in the previous versions of googleVis.
Change the tag to 'chart'
and plot.gvis
will produce the same output as print.gvis
.
Thus, setting the gvis.plot.tag
value to 'chart'
in options()
will return the HTML code of the chart when the file is parsed with knitr
.
See the example in ?plot.gvis
for more details
library(slidify)
setwd("~/Dropbox/Lancaster/")
author("Introduction_to_googleVis")
## Edit the file index.Rmd file and then
slidify("index.Rmd")
googleVis brings interactive plots to R
Use them to engage with other
No more boring data
sessionInfo()
## R version 3.0.1 (2013-05-16)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
##
## locale:
## [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] XML_3.95-0.2 RJSONIO_1.0-3 googleVis_0.4.3 slidify_0.3.3
##
## loaded via a namespace (and not attached):
## [1] digest_0.6.3 evaluate_0.4.3 formatR_0.7 knitr_1.2
## [5] markdown_0.5.4 stringr_0.6.2 tools_3.0.1 whisker_0.3-2
## [9] yaml_2.1.7