Google Charts in R Markdown

A quick look at the documentation showed that it’s very easy indeed to do this sort of thing.


Suppose, for example, that you want to illustrate to students the risks associated with extrapolation. You begin by reminding them of the experience they had back in high school with their graphing calculators, when they zoomed in on a curve: zoom in close enough, and it looks like a straight line.

Then you point out that for the most part we live our lives from a “zoomed-in” perspective, at least where data is concerned. In situations where we are interested in a pair of numerical measurements on individuals, we usually possess \(y\)-values for only a fairly narrow range of \(x\)-values. Hence it is likely that a scatter plot we make from our “zoomed-in” data will show a roughly linear relationship, even though on a global scale the “real” relationship probably is some kind of a curve.

The app below (a slight modification of the example in Gesmann’s post) makes the point in a flash. Click and drag to establish a zoom region, right-click to reset:

All we needed was the following code (be sure to add the chunk option results='asis'):

x <- seq(0,100,by=0.5)
y <- (50-x)^2+rnorm(length(x),sd=100)

curvy <- data.frame(x,y)

gvScat <- gvisScatterChart(curvy,
                     explorer="{actions: ['dragToZoom',
                     hAxis="{title: 'Explanatory x',
                     titleTextStyle: {color: '#000000'}}",
                     vAxis="{title: 'Response y',
                     titleTextStyle: {color: '#000000'}}",
                     title="Curvilinear Relationship",
                     width=550, height=500,


The same approach works in any R Markdown document (including the source document for this Jekyll-powered post). I will certainly take a closer look at googleVis: thanks, Markus!