Using googleVis via R provides lots of options to create nice google visualizations. I was trying to create some charts while exploring the Annual Nominal Fish Catches Data on Kaggle. I wanted to create a line motion chart and exclude the default bubble chart. So I played with the options to get the desired result. The following is a quick explanation of how to do that.
|
|
Fish Catches Dataset
The dataset provides the annual TLW (tonnes live weight) catches of fish and shellfish in the Northeast Atlantic region. The original data, after cleaning, is in a wide format as follows:
## Species Area Units Country 2014 2013 2012 2011 2010 2009 2008 2007
## 1 ANF 27 TLW BE 993 1633 1716 1279 1031 853 964 1363
## 2 ANF 27.4 TLW BE 217 137 133 116 131 140 185 181
## 3 ANF 27.4.A TLW BE 0 0 0 0 0 0 0 0
## 2006
## 1 1193
## 2 141
## 3 0
Since gvisMotionChart() requires a time variable, I melted the dataframe to convert it to a long format as follows:
## Species Area Units Code Year Quantity Country
## 1 ANF 27 TLW BE 2014 993 Belgium
## 2 ANF 27.4 TLW BE 2014 217 Belgium
## 3 ANF 27.4.A TLW BE 2014 0 Belgium
Then I wanted to plot the annual fish catch per country and also per species, so I calculated the annual sums and got the following:
Annual Fish Catch per Country
## Source: local data frame [216 x 3]
## Groups: Year [9]
##
## Year Country TLW
## (dbl) (chr) (int)
## 1 2006 Belgium 67564
## 2 2006 Denmark 2680872
## 3 2006 Estonia 296160
## 4 2006 Faroe Islands 2597422
## 5 2006 Finland 451731
## 6 2006 France 1310819
## 7 2006 Germany 884707
## 8 2006 Greenland 150961
## 9 2006 Iceland 5353078
## 10 2006 Ireland 698563
## .. ... ... ...
Annual Fish Catch per Species
## Source: local data frame [8,181 x 3]
## Groups: Year [9]
##
## Year Species TLW
## (dbl) (chr) (int)
## 1 2006 AAS 0
## 2 2006 ABK 0
## 3 2006 ABX 20
## 4 2006 ABZ 0
## 5 2006 ACC 8
## 6 2006 ACH 0
## 7 2006 AES 0
## 8 2006 AGD 0
## 9 2006 AGK 15
## 10 2006 AGN 0
## .. ... ... ...
googleVis Line Motion Chart Options
The default gvisMotionChart requires a dataframe with at least four columns: subject name (idvar), time (timevar) and two columns of numeric values. This is mainly for the bubble chart, which does not work properly with three columns. But since the line/bar charts can be used with 3 columns, I could use it with my summarized data. But I wanted, first to set the default view to line chart., and second to ** hide the tabs of the other charts**.
To achieve these two features, I played with the options of gvisMotionChart as follows:
-
To view the the line chart by default, I set “iconType”:“LINE” in State; which is passed to the function.
-
To hide the other charts, I set showChartButtons=F
I also wanted to have unique colors for the lines in the initial view, so I set “colorOption”:"_UNIQUE_COLOR" .
And that’s how I got a line motion chart.
#set state options to pass to gvisMotionChart
State<-'
{"yLambda":1,"showTrails":false,"xAxisOption":"_TIME",
"xZoomedDataMin":1136073600000,
"orderedByY":false,"playDuration":15000,
"nonSelectedAlpha":0.4,"xZoomedIn":false,"xLambda":1,
"colorOption":"_UNIQUE_COLOR",
"iconKeySettings":[],
"xZoomedDataMax":1388534400000,
"dimensions":{"iconDimensions":["dim0"]},
"sizeOption":"_UNISIZE","yZoomedIn":false,
"uniColorForNonSelected":false,"time":"2014",
"yZoomedDataMax":10000000,
"iconType":"LINE",
"duration":{"timeUnit":"Y","multiplier":1},
"yAxisOption":"2","yZoomedDataMin":0,"orderedByX":false}
'
#create the country chart
CountryChart = gvisMotionChart(datm.country, "Country", "Year",
options = list(showChartButtons=F, state=State))
#create the species chart
SpeciesChart= gvisMotionChart(datm.species, "Species", "Year",
options = list(showChartButtons=F, state=State))
#Plot the 2 charts
BothCharts<-gvisMerge(CountryChart,SpeciesChart)
#remove caption
BothCharts$html$caption=""
plot(BothCharts)
The data and the full code can be found Here