Google Charts from R

Late last year Google released the Google Chart API, which gives programmers access to a web service that renders charts. So this url

http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World

will produce a chart like this:

Try it yourself -- copy the url into your browser; change the text from "Hello World" to something else, etc. And the API supports bar plots, line charts, Venn diagrams (!) and even, recently, maps.

People have written libraries in various languages to provide interfaces to the API (here's a list of them), and tonight I hacked together a little R interface to the scatterplot charts. It's quite rough, but I'd be curious if anyone wants to extend it or can show anything cool with it.

From R, all you have to do is:

> source("http://people.fas.harvard.edu/~aeggers/code/googleplot.r")

And then where you might say

> plot(1:9, c(4,2,4,3,6,4,7,8,5), cex = 1:9, xlim = c(0, 10), ylim = c(1,10))

you can use the same syntax with the googleplot function

> googleplot(1:9, c(4,2,4,3,6,4,7,8,5), cex = 1:9, xlim = c(0, 10), ylim = c(1,10))

and get back a long url encoding those parameters

"http://chart.apis.google.com/chart?cht=s&chd=s:GMSYekqw2,SGSMeSkqY,
GNUbhov29&chxt=x,y&chxl=0:|0|2|5|7|10|1:|1|3|5|7|10&chs=250x200"

which, when entered into an address bar or embedded in an img tag in a web page, gives you a figure like this:

It seems like this approach could provide a convenient way to publish a figure on the web in some circumstances, but setting aside the insufficiency of my R function, the graphics flexibility of the API isn't quite large enough yet (eg can't pass an axis label, ie xlab in R). In most cases it seems like you'd just want to create a nice PNG in R or whatever and then publish that. But I'd love to hear if anyone finds a way to use this or thinks it'is worth extending further.

Posted by Andy Eggers at April 2, 2008 1:07 AM