April 2008
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Authors' Committee

Chair:

Matt Blackwell (Gov)

Members:

Martin Andersen (HealthPol)
Kevin Bartz (Stats)
Deirdre Bloome (Social Policy)
John Graves (HealthPol)
Rich Nielsen (Gov)
Maya Sen (Gov)
Gary King (Gov)

Weekly Research Workshop Sponsors

Alberto Abadie, Lee Fleming, Adam Glynn, Guido Imbens, Gary King, Arthur Spirling, Jamie Robins, Don Rubin, Chris Winship

Weekly Workshop Schedule

Recent Comments

Recent Entries

Categories

Blogroll

SMR Blog
Brad DeLong
Cognitive Daily
Complexity & Social Networks
Developing Intelligence
EconLog
The Education Wonks
Empirical Legal Studies
Free Exchange
Freakonomics
Health Care Economist
Junk Charts
Language Log
Law & Econ Prof Blog
Machine Learning (Theory)
Marginal Revolution
Mixing Memory
Mystery Pollster
New Economist
Political Arithmetik
Political Science Methods
Pure Pedantry
Science & Law Blog
Simon Jackman
Social Science++
Statistical modeling, causal inference, and social science

Archives

Notification

Powered by
Movable Type 4.24-en


« Simulating Major League Baseball | Main | A born-again frequentist? »

2 April 2008

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