Setting the initial view of a motion chart in R

[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Following on from my article about accessing and plotting World Bank data with R I want to talk about how to change the initial view of a motion chart.

Over the last couple of weeks I have been asked a view times how to do this. For instance Stephen O’Grady wanted to create a motion chart, which shows initially a line chart, rather than a bubble chart.

Changing the initial settings of a motion chart is actually quite easy, if you know how to. The trick is to use the state argument in the list of options of gvisMotionChart.

As a case study we will come back to the World Bank data set and try to do some homework givem by Duncan Temple Lang in his course on introduction to statistical computing course by. Duncan asked his students to query the World Bank data base to create a plot, which would show the number of internet users per 1000 in Africa over time as line chart. Further, he would like to see a legend next to the chart to identify which country is which and tooltips for each curve, so we can identify the country.

A motion chart, displayed as a line chart, would do the trick.

Okay, getting the data is easy, thanks to the WDI package, or via a direct download, and so it is to create a motion chart with bubbles. Interactively we can change the bubble chart into a line chart, we can select some countries and change the y-axis to log-scale. However, when we reload the page we are back to square one: a bubble chart. So the idea is to pass the changed chart setting on to the initial plot. You find those settings, of the current view, as a string in the advanced tab of the settings window. Click on the wrench symbol in the bottom right hand corner of a motion chart to access this window.

Copy this string and paste it into the state argument of the options list.

Here is an example, where I pre-selected Sierra Leone and Seychelles (countries with the lowest and highest number of internet users) together with Africa, North Africa and Sub-Saharan Africa (all income levels). You find the R code below to replicate the plot.

What does the data tell you, and how could we improve the plot?

For more details about the googleVis package see also the vignette.

## Based on ideas and code from Duncan Temple Lang, see:
url = ""
iu = fromJSON(url)
internetUsers = data.frame(year = as.numeric(sapply(iu[[2]], "[[", "date")),
  InternetUsersPerThousands = as.numeric(sapply(iu[[2]],
   function(x) ifelse(is.null(x[["value"]]),  NA, x[["value"]]))),
  country = sapply(iu[[2]], function(x)
## Create a line plot with gvisMotionChart
## Set initial state with a few regions selected and a log y-axes
myState <- '
 "iconKeySettings":[{"key":{"dim0":"Sub-Saharan Africa (all income levels)"}},
                    {"key":{"dim0":"North Africa"}},
                    {"key":{"dim0":"Sierra Leone"}},
M <- gvisMotionChart(internetUsers, "country", "year", 
  options=list(width=650, height=350, state=myState))
## Session Info
## sessionInfo()
## R version 2.13.1 Patched (2011-08-14 r56742)
## Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
## locale:
## [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## other attached packages:
## [1] googleVis_0.2.10 RJSONIO_0.95-0 
## loaded via a namespace (and not attached):
## [1] tools_2.13.1

To leave a comment for the author, please follow the link and comment on their blog: mages' blog. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)