Markdown example with knitr and googleVis

This is a little Markdown example file. It uses googleVis package with knitr and markdown to produce interactive plots from Google Visualization API.

In this case change the behaviour of plot.gvis, so that it presents only the code for the chart rather than making a full web page.

library(googleVis)
op <- options(gvis.plot.tag = "chart")

The following plot statements will automatically return the HTML required for the 'knitted' output.

Pie chart

example pie charts

Let's take a look at the data:

head(CityPopularity)
##          City Popularity
## 1    New York        200
## 2      Boston        300
## 3       Miami        400
## 4     Chicago        500
## 5 Los Angeles        600
## 6     Houston        700

Now plot the pie chart


Pie <- gvisPieChart(CityPopularity, options = list(width = 400, height = 200))
plot(Pie)

Place two charts next to each other

Example of a gvisGeoChart with gvisTable Let's have a look at the data first

head(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
Geo <- gvisGeoChart(Exports, locationvar = "Country", colorvar = "Profit", options = list(height = 300, 
    width = 350))
Tbl <- gvisTable(Exports, options = list(height = 300, width = 200))
plot(gvisMerge(Geo, Tbl, horizontal = TRUE))

Scatter Plot

Scatter plot example with googleVis

head(women)
##   height weight
## 1     58    115
## 2     59    117
## 3     60    120
## 4     61    123
## 5     62    126
## 6     63    129

This time we will be able to edit the plot since we set gvis.editor argument.


Scatter1 <- gvisScatterChart(women, options = list(gvis.editor = "edit", vAxis = "{title:'weight (lbs)'}", 
    hAxis = "{title:'height (in)'}"))
plot(Scatter1)

Intensity Map

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

Intensity1 <- gvisIntensityMap(df, locationvar = "country", numvar = c("val1", 
    "val2"))
plot(Intensity1)

Combo chart

## Add the mean
CityPopularity$Mean=mean(CityPopularity$Popularity)
CC <- gvisComboChart(CityPopularity, xvar='City',
          yvar=c('Mean', 'Popularity'),
          options=list(seriesType='bars',
                       width=450, height=300,
                       title='City Popularity',
                       series='{0: {type:\"line\"}}'))
plot(CC)
## Set options back to original options
options(op)

Session info

sessionInfo()
## R version 3.0.1 (2013-05-16)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] googleVis_0.4.5 knitr_1.4.1    
## 
## loaded via a namespace (and not attached):
## [1] digest_0.6.3   evaluate_0.4.7 formatR_0.9    RJSONIO_1.0-3 
## [5] stringr_0.6.2  tools_3.0.1