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.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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: ## http://eeyore.ucdavis.edu/stat242/Homeworks/XML.html library(googleVis) url = "http://api.worldbank.org/countries/all/indicators/IT.NET.USER.P3?date=1990:2010&format=json&per_page=12000" 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) x[["country"]]['value'])) ## Create a line plot with gvisMotionChart ## Set initial state with a few regions selected and a log y-axes myState <- ' {"yZoomedIn":false,"yZoomedDataMin":0,"xAxisOption":"_TIME", "xZoomedDataMax":1230768000000,"time":"2009","playDuration":15000, "yAxisOption":"2","yLambda":0,"duration":{"timeUnit":"Y","multiplier":1}, "xZoomedDataMin":631152000000,"uniColorForNonSelected":false, "showTrails":false,"orderedByY":false,"nonSelectedAlpha":0, "orderedByX":false,"sizeOption":"_UNISIZE","xLambda":1, "colorOption":"_UNIQUE_COLOR", "iconKeySettings":[{"key":{"dim0":"Sub-Saharan Africa (all income levels)"}}, {"key":{"dim0":"North Africa"}}, {"key":{"dim0":"Seychelles"}}, {"key":{"dim0":"Sierra Leone"}}, {"key":{"dim0":"Africa"}}], "dimensions":{"iconDimensions":["dim0"]}, "yZoomedDataMax":220,"xZoomedIn":false,"iconType":"LINE"} ' M <- gvisMotionChart(internetUsers, "country", "year", options=list(width=650, height=350, state=myState)) plot(M) ## 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.
R-bloggers.com 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.