One-liners which make me love R: Make your data dance (Hans Rosling style) with googleVis #rstats

It may be a cliché, but much of R’s utility comes from its amazing community. And by community, I am specifically referring to the bright, hard-working people who are willing to share their knowledge and code with the rest of us. Because of their contributions, we can do some amazingly cool and useful things with very little code of our own. It is in this context that I launch this new series to highlight packages and functions which make it easy to do jaw-droppingly cool and useful things.

First up: the googleVis package by Markus Gesmann and Diego de Castillo which makes it easy — often with just one-line of R — to harness the Google Visualization API. Annotated timelines, gauges, maps, org charts, tree maps, and more are suddenly at your command.

I’m going to focus on the motion chart, popularized by Hans Rosling in his groundbreaking 2006 TED talk on global economic development. (If you haven’t seen it yet, you should. Right now. Seriously. Go.) Motion charts are an innovative way to display multidimensional time series in an interactive way. And the googleVis package even comes with some sample data to make it even easier to try them out.

The package is available from CRAN if you need to install it.

To get started, load the package and the included “Fruits” data.frame:

library(googleVis)
data(Fruits)

This data.frame contains some sample data about sales of various fruits at different locations for different years. There’s even a proper Date column already constructed for us from the numeric Year column:

To make the chart, we need to give the gvisMotionChart() function our data.frame and tell it a few things about it: the column which identifies the items to examine (idvar=Fruit), the time dimension (timevar=Date), and optionally a name to use to identify the chart in the generated HTML and JavaScript (we’ll use chartid="ILoveFruits"):

M = gvisMotionChart(data=Fruits, idvar="Fruit", timevar="Date", chartid="ILoveFruit")

That’s it.

You can view your chart with the overridden plot() function. It will automatically spawn a browser window and serve up your chart through R’s internal web server:

plot(M)

Since WordPress doesn’t allow embedded JavaScript, please click through to see the motion chart in action:

You can also access all 165 lines of the generated HTML and JavaScript and save it to disk:

cat(unlist(M$html), file="output/ILoveFruits.html")

Time suck alert: googleVis may make them easy to create, but motion charts can be a lot of fun to play with. You have been warned…

If you want to take a look at an example with some real data, you might be interested in the 20 Years of the U.S. Domestic Airline Market In 20 seconds post on my work blog.

Finally, here are the slides from my lightning talk on this topic at this month’s Greater Boston useR Group meeting:

Have fun!

About these ads

9 Responses to “One-liners which make me love R: Make your data dance (Hans Rosling style) with googleVis #rstats”

  1. Markus Says:

    Hi Jeffery,

    Thanks for the post. The new version (0.2.7) of googleVis has a section in the vignette explaining how to use the package output with WordPress. Basically you need a plugin, e.g. “custom fields shortcode”, for more details and an example see:

    http://code.google.com/p/google-motion-charts-with-r/wiki/UsingWordPress

    Cheers,

    Markus

    • Jeffrey Breen Says:

      Hi Markus:

      Thanks for dropping by — and thanks especially for the googleVis!

      I found some info on the custom fields plugin, but I got the impression it’s only available for self-hosted WordPress blogs. For once in my (IT) life, I’ve taken the easy route and am using WordPress.com’s hosted service. (That’ll teach me!) But I’m sure it will help the WordPress.org’ers out there!

      Thanks again,
      Jeffrey

  2. R & GoogleVis #2 | Яante Says:

    [...] questo post di Jeffrey Breen che ha come oggetto l’integrazione di R con Google Visualization API [...]

  3. Use Dropbox’s public folder for web publishing via Notepad (or emacs or…) « Things I tend to forget Says:

    [...] on T…Abhijit Sanyal on slides from my R tutorial on T…R & GoogleVis #2… on One-liners which make me love …Jeffrey Breen on installing R 2.13.1 on Amazon …Jeffrey Breen on installing R 2.13.1 on [...]

  4. Alexander Rafael Says:

    The last line creates an empty html file.

  5. Wait til the Weekend at johnramey Says:

    [...] One-liners which make me love R: Make your data dance (Hans Rosling style) with googleVis [...]

  6. valugi Says:

    That title is somehow misleading. That oneliner of R has a lot of lines of R, C, C++, or FORTRAN in the back.
    It depends where the observer is positioned in the production chain between the final visualization and the raw data.

    • Jeffrey Breen Says:

      Sorry I missed this in the comment queue–

      That’s a great point, but it’s also my point (which I could have made clearer): much of the power of R comes from the generous contributions of others in the community. What I like about R is that I can do very complicated, sophisticated, and cool things without having to do much (or any!) of the heavy lifting myself.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: