Creating Animated Plots in R

INTRODUCTION

Animating a visualization brings a new dimension to our visualization. It helps to be a good storyteller, but when the story is accompanied by animation or a plot, it adds force to the story. Many This document will help you to create interactive and animated plots using the googleVis package.

Getting ready

We need to install and load the googleVis package in R:

install.packages("googleVis")

library(googleVis)

Next we will get our data file. We have downloaded data from the World Bank website.

Reading the data file

We will first import the final.csv file, which contains our data in R, using the read.csv() function:

data1 = read.csv("final.csv")

We can examine the data using the head(emisn) function or View(emisn)

head(data1)

View(data1)

 

Getting the motion chart

We can now generate the googleVis object for animated visualizations using the gvisMotionChart() function and execute it using the plot() function. Note that this
visualization opens up a new browser and hence requires Internet connectivity:

chart<- gvisMotionChart(data1, idvar="Country", timevar="Years", xvar = "fert",yvar = "life")

plot(chart)

When we execute the preceding code, R will open a new window with an interactive screen. The googleVis package provides its users with the option to switch between bubble, bar, and line charts. The play button at the bottom left will start the animation. Users can also select specific countries from the list and observe how they progressed over time in relation to similar economies. The color drop-down menu will allow users to change the colors of bubbles.

The gvisMotionchart() function requires the data to be in a data frame format and it is used as its first argument. In our case, data1 is passed as the first argument. The second argument is the idvar argument, which is the column to be analyzed; in our case, this would be country. The third argument, timevar, adds the time dimension to our plot. The xvar and yvar arguments are self-explanatory. We can also add the sizevar as well as the colorvar argument. These two arguments, along with the options argument are explained in the googleVis manual available at http://cran.r-project.org/web/packages/googleVis/googleVis.pdf.

Critical thinking

Interpret the motion chart.