Tinniam V. Ganesh Tinniam V. Ganesh - 2 months ago 23
R Question

gVisMotionChart gives issue with unique rows

I am trying to use gVisMotion Chart but I get the error

gg=gvisMotionChart(data, idvar='NY.GNP.PCAP.CD', timevar='year',options=list())
Error in gvisCheckMotionChartData(data, my.options) :
The data must have rows with unique combinations of idvar and timevar.
Your data has 9982 rows, but idvar and timevar only define 6328 unique rows.


This is what I am doing

dat = WDI(indicator='NY.GNP.PCAP.CD', country="all",start=1970, end=2016)
l = dim(dat)
#Subset countries
data <- dat[2163:l[1],]
gg=gvisMotionChart(data, idvar='NY.GNP.PCAP.CD', timevar='year',options=list())


and I get the error

Error in gvisCheckMotionChartData(data, my.options) :
The data must have rows with unique combinations of idvar and timevar.
Your data has 9982 rows, but idvar and timevar only define 6328 unique rows.

> head(data)
iso2c country NY.GNP.PCAP.CD year
2163 AF Afghanistan 630 2015
2164 AF Afghanistan 670 2014
2165 AF Afghanistan 730 2013
...


It is quite likely that NY.GNP.PCAP.CD & year have some repeating rows for different countries. Isn't it?

What am I doing wrong? How can this be fixed

Answer

idvar='NY.GNP.PCAP.CD' should be changed to idvar='country'. The intuition being that different countries (or even the same country) could have the same GNP over the course of different years -thats why you get that error. Running this code works.

data <- dat[2163:l[1],]
  gg=gvisMotionChart(data, idvar='idvar='country'', timevar='year',options=list())