Contributed by David Comfort. David took NYC Data Science Academy 12 week full time Data Science Bootcamp pr... between Sept 23 to Dec 18, 2015. The post was based on his first class project(due at 2nd week of the program).
Hans Rosling gave a famous TED talk in 2007, “The Best Stats You’ve Ever Seen”. Rosling is a Professor of International Health at the Karolinska Institutet in Stockholm, Sweden and founded the Gapminder Foundation.
To visualise his talk, he and his team at Gapminder developed animated bubble charts, also known as motion charts. Gapminder developed the Trendalzyer data visualization software, which was subsequently acquired by Google in 2007.
The Gapminder Foundation is a Swedish NGO which promotes sustainable global development by increased use and understanding of statistics about social, economic and environmental development.
The purpose of my data visualization project was to visualize data about long-term economic, social and health statistics. Specifically, I wanted to extract data sets from Gapminder using an R package, googlesheets, munge these data sets, and combine them into one dataframe, and then use the GoogleVis R package to visualize these data sets using a Google Motion chart.
The finished product can be viewed at this page and a screen capture demonstrating the features of the interactive data visualization is at:
World Bank Databank
The following Gapminder datasets, several of which were adapted from the World Bank Databank, were accessed for data visualization:
gs_url()return a registered sheet as a
googlesheetsobject, which is the first argument to practically every function in this package.
First, install and load
dplyr, and access a Gapminder Google Sheet by the URL and get some information about the Google Sheet:
Note: Setting the parameter
lookup=FALSE will block authenticated API requests.
A utility function,
extract_key_from_url(), helps you get and store the key from a browser URL:
You can access the Google Sheet by key:
Once, one has registered a worksheet, then you can consume the data in a specific worksheet (“Data” in our case) within the Google Sheet using the
gs_read() function (combining the statements with
dplyr pipe and using
check.names=FALSE so it deals with the integer column names correctly and doesn’t append an “x” to each column name):
You can also target specific cells via the
range = argument. The simplest usage is to specify an Excel-like cell range, such as
range = “D12:F15” or
range = “R1C12:R6C15”.
But a problem arises since
check.names=FALSE does not work with this statement (problem with package?). So a workaround would be to pipe the data frame through the dplyr
However, for purposes, we will ingest the entire worksheet and not target by cells.
Let’s look at the data frame. We need to change the name of the first column from “GDP per capita” to “Country”.
We need to change the name of the first column from “GDP per capita” to “Country”.
Let’s download the rest of the datasets
We want to segment the countries in the data sets by region and sub-region. However, the Gapminder data sets do not include these variables. Therefore, one can download the ISO-3166-Countries-with-Regional-Codes data set from github which includes the ISO country code, country name, region, and sub-region.
rCurl to read in directly from Github and make sure you read in the “raw” file, rather than Github’s display version.
Note: The Gapminder data sets do not include ISO country codes, so I had to clean the countries data set with the corresponding country names used in the Gapminder data sets.
We need to reshape the data frames. For the purposes of reshaping our data frames, we can divide the variables into two groups: identifier, or id, variables and measured variables. In our case, id variables include the Country and Years, whereas the measured variables are the GDP per capita, life expectancy, etc..
We can further abstract and “say there are only id variables and a value, where the id variables also identify what measured variable the value represents.”
For example, we could represent a data set, which has two id variables, subject and time:
where each row represents one observation of one variable. This operation is called melting (and can be achieved by using the
melt function of the Reshape package).
Compared to the former table, the latter table has a new id variable “variable”, and a new column “value”, which represents the value of that observation. See the paper, Reshaping data with the reshape package, by Hadley Wickham, for more clarification.
We now have the data frame in a form in which there are only id variables and a value.
Let’s reshape the data frames ( child_mortality, democracy_score, life_expectancy, population):
Whew, now we can finally all the datasets using a
The name of the visualization function is
ChartType. So for the Motion Chart we have:
The output of a
googleVis function (
gvisMotionChart in our case) is a list of lists (a nested list) containing information about the chart type, chart id, and the html code in a sub-list split into header, chart, caption and footer.
Let’s plot the GoogleVis Motion Chart.
[caption id="attachment_8127" align="alignnone" width="650"] Gapminder Data Visualization using GoogleVis[/caption]
Note: I had an issue with embedding the GoogleVis motion plot in a WordPress blog post, so I will subsequently feature the GoogleVis interactive motion plot on a separate page at http://adatascience.com/Gapminder_Data_Visualization.html
Here is a screen recording of the data visualization produced by GoogleVis of the Gapminder datasets:
googlesheetsR package allows for easy extraction of data sets which are stored in Google Sheets.