Interactive Line Charts

In the current post we will go over a step by step process on how to make an interactive line chart in R using googleVis package. Line charts are widely used for displaying trend in data.

FDI in Auto
Foreign Direct Investment in India

Data :

The data for the current post can be downloaded here. The data comprises of two columns year and fdi. The fdi column represents the amount of equity inflow in the auto industry in India from 2000 to 2016. Note that i have transformed the data from its original form.

How to make a chart?

We will start by creating a simple chart and then add elements to it to make it better. For the purpose of this post we will download the googlVis package and load the same in current R session.

install.packages("googleVis")
library(googleVis)
fdi = read.csv("auto.csv", stringsAsFactors = FALSE)
fdi.plt.dflt = gvisLineChart(fdi)
plot(fdi.plt.dflt)

In the above code snippet we read the CSV file using the read.csv() function. The first argument is the name of the csv file that contains the data. In our case the file is stored as auto.csv. The second argument is we set stringAsFactor = FALSE, this argument will not convert string into factors.

Now, to generate a simple line chart in R we will use the gvisLineChart() function. The only argument in this function is the data set. Since the data is already formatted the function will use the first column and plot it on the X axis and will use the second column to plot it on the Y axis. Note that in this case we are creating an R object fdi.plt.dflt which holds the necessary information. We now use the plot() function from base R package and pass fdi.plt.dflt object as an argument. R will now open a new window using your default browser and generate the plot.

The plot generate above will use all the default settings of gvisLineChart(). Now let us clean the plot and add necessary information.

fdi.plt.1 = gvisLineChart(fdi, options=list(width=600, height=500))
plot(fdi.plt.1)

In the above code we are using the same function but we introduce the options argument. The options argument will allow us to add styles and elements to our current plot and make it more informative. The options argument uses a list() which takes a long list of arguments to add styles to the line chart. To explore all the arguments available for line chart visit the googleVis package developers website. In the current scenario we are going to edit the width and height of the default plot by setting width to 600 pixels and height to 500 pixels.

Now we have a nicer looking plot. But, i think we can make it look better by adding removing the legend and adding a background color.

fdi.plt.2 = gvisLineChart(fdi, options=list(width=600, height=500, 
                          backgroundColor='#efedf5',
                          legend ='none')) 
plot(fdi.plt.2)

In the above code we have everything as is but added backgroundColor = ‘hex value’ and removed the legends by legend = ‘none’.

If we visit the developer website we see that the backgroundColor has been specified with a type: string or object and the Default : ‘white’. Now, this tells us that to pass a different background color we need to use single quote and either a hex value or actual color name. I prefer hex value- backgroundColor=’#efedf5′ thus we give a new background color. Now lets do the same exercise for legend by going to the developers website. Since we are plotting a simple line chart we do not need a legend we can request R to avoid adding a legend by using legend=’none’ argument.

Next we plot the line chart using the plot() function in R. You will notice a few changes from the prior line chart.

Final Plot:

Now let us make the plot with final necessary changes 1) fix axes tickmarks  and 2) title of the plot. We can do this using the following code:

class(fdi$year)
class(fdi$fdi)
fdi$year= as.numeric(fdi$year)

fdi.plt.3 = gvisLineChart(fdi, options=list(width=600, height=500, 
                                            backgroundColor='#efedf5',
                                            legend ='none',
                                            title="FDI investment in Automotive Sector in India",
                                            hAxis= "{title:'Years',ticks:[2000,2005,2010,2015]}",
                                            vAxis="{title:'US $ Million',ticks:[250,500,750,1000,1250,1500,1750,2000,2250,2500,2750,3000]}"))
plot(fdi.plt.3)

The first step to customizing the axes tickmarks is to ensure that the year variable in the data is numeric. we can check the class of a variable simply by using the class() function in R. The class in our case is not numeric for year variable, hence we change the class of year variable to numeric using as.numeric(). Now to add a custom horizontal axis tickmarks we pass the following argument:

hAxis= "{title:'Years',ticks:[2000,2005,2010,2015]}",

Note the use of {} braces and [] they both are used to ensure that the plot accepts tickmarks. Also note that the hAxis ues an “=” sign but the title and ticks would use “:”. We repeat the same for vertical axis tickmarks and title.

We should also be aware that the arguments within options () function are separated uing a comma except the last. Hence, we do not use a comma after vAxis statement.

What Next:

Once we have the line chart ready we can either take a snapshot or embed it within a shiny application ora Rmarkdown file. Will cover this in the next tutorial.

Conclusion:

I prefer line charts for displaying trends in data. The advantage of using an interactive  line chart is to remove the guess work when there are too many points. One can easily move the mouseover and get the information regarding the data. We will definitely explore this function in coming videos.

References:

  1. New York Times graphics of basebal

Code:

library(googleVis)
library(dplyr)

setwd("/Users/atmajit/open_data_India/fdi data india")

fdi = read.csv("auto.csv", stringsAsFactors = FALSE)
fdi.plt.dflt = gvisLineChart(fdi)
plot(fdi.plt.dflt)

fdi.plt.1 = gvisLineChart(fdi, options=list(width=600, height=500)) # width and hieght are in Pixels.
plot(fdi.plt.1)

fdi.plt.2 = gvisLineChart(fdi, options=list(width=600, height=500, 
 backgroundColor='#efedf5',
 legend ='none')) 
plot(fdi.plt.2)

class(fdi$year)
class(fdi$fdi)
fdi$year= as.numeric(fdi$year)
fdi.plt.3 = gvisLineChart(fdi, options=list(width=600, height=500, 
 backgroundColor='#efedf5',
 legend ='none',
 title="FDI investment in Automotive Sector in India",
 hAxis= "{title:'Years',ticks:[2000,2005,2010,2015]}",
 vAxis="{title:'US $ Million',ticks:[250,500,750,1000,1250,1500,1750,2000,2250,2500,2750,3000]}"))
plot(fdi.plt.3)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s