Google Charts in R Markdown03 Sep 2014
An excellent little post (Zoom, zoom googleVis) showed up recently on R-Bloggers. The author Markus Gesmann is the maintainer of the
googleVis package that links R to the Google Charts API. My first thought was: could I embed charts like those in R Markdown documents that could knit to ioslides or other formats suitable for use in my elementary statistics classes?
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 -values for only a fairly narrow range of -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
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!