| 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 | 31 |
« Lee Fleming on "Mobility, Skills, and the Michigan Noncompete Experiment" | Main | JAMA article on ghostwriting medical studies »
15 April 2008
A few weeks ago I wrote a post sharing some code I wrote to generate sharp-looking PNG scatterplots from R using the Google Chart API. I think there are some nice uses of that (for example, as suggested by a commenter, to send a quick plot over IM), but here's something that I think could be much more useful: maps from R using Google Charts.
So, suppose you have data on the proportion of people who say "pop" (as opposed to "soda" or "coke") in each US state. (I got this data from Many-Eyes.) Once you get my code, you enter a command like this in R
googlemap(x = pct_who_say_pop, codes = state_codes, location = "usa", file ="pop.png")
and this image is saved locally as "pop.png":
To use this, first get the code via
source("http://people.fas.harvard.edu/~aeggers/googlemap.r")
which loads in a function named googlemap, to which you pass
For optional parameters to affect the scale of the figure and its colors, see the source.
Another quick example:
Suppose you wanted to make a little plot of Germany's colonial possessions in Africa. This code
googlemap(x = c(1,1,1,1), location = "africa", codes = c("CM", "TZ", "NA", "TG"),file = "germans_in_africa.png")
returns this url
"http://chart.apis.google.com/chart?cht=t&chtm=africa . . . etc.
and saves this PNG on your hard drive:
The scatterplot thing before was something of a novelty, but I think this mapping functionality could actually be useful for generating quick maps in R, since the existing approaches are pretty annoying in my (limited) experience. The Google Charts API is not very flexible about labels and whatnot, so you probably won't be publishing any of these figures. But I expect this will serve very well for quick exploratory stuff, and I hope others do too.
I'd love it if someone wanted to help roll this into a proper R package . . . .
Posted by Andy Eggers at April 15, 2008 3:01 PM
This is very cool. Thanks for sharing the code.
Posted by: Dave at April 15, 2008 6:04 PM
Building an R package is quite easy (at least for MS-Windows):
1. Download and install the R-Tools.
2. Start R and load googlemap.R and googleplot.R.
3. Type package.skeleton("googleR", path = "yourPath:/")
4. Go to your newly created yourPath\googleR; in my case it's d:\googleR and edit DESCRIPTION, googlemap.Rd and googleplot.Rd (both in man/). If you know LaTeX this shouldn't be a problem.
5. Start Microsoft's command line ("MS-DOS"), go to "yourPath" and type in "R CMD build --binary --use-zip googleR". That's it.
Here's the source-file (googleR.zip) and here's your new (MS-Windows) R package googleR (googleR_1.0.zip)...
BTW, writing an appropriate documentation is probably the main task...
Posted by: Bernd at April 15, 2008 11:35 PM
Thanks for that information, Berndt. This is really useful. I will think about how much more I want to do with the code before releasing a package -- it's tempting to try to include other chart types, more flexibility with each plot, etc.
Posted by: Andy Eggers
at April 18, 2008 9:12 AM
Very Cool Stuff, this has many uses for me, thanks for sharing the code!!
Jason Botts
Posted by: Jason Botts at April 18, 2008 9:24 PM
Wow, what a great way to combine R and Google. Thanks for sharing, I will definitely give it a try. Excellent!
Posted by: Bertolt Meyer at April 21, 2008 3:54 AM