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 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 (
M = gvisMotionChart(data=Fruits, idvar="Fruit", timevar="Date", chartid="ILoveFruit")
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:
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: