绘制世界银行数据:条件长度> 1

时间:2012-04-12 06:56:01

标签: r google-visualization

我正在尝试使用googleVis包为R绘制一些世界银行数据,但它给了我以下错误:

In if (class(x[[.x]]) == "Date") as.character(x[[.x]]) else x[[.x]] :
the condition has length > 1 and only the first element will be used

我使用了以下代码:

# load the packages
library(WDI) # package for importing World Bank data
library(plm) # package for working with panel data
library(googleVis) # the googleVis package

# import the data using the WDI package
LONG <- WDI(country=c("AGO","BEN","BWA","BFA","BDI"), indicator=c("SP.DYN.CBRT.IN",
"SP.DYN.TFRT.IN", "SP.POP.TOTL", "NY.GDP.PCAP.KN"), start=2005, end=2009, 
extra=FALSE)

# transform to panel format and encode the year variable as numeric
PANEL <- pdata.frame(LONG, c("country","year"))
PANEL$year <- as.numeric(as.character(PANEL$year))

# plot in a MotionChart using googleVis
MC <- gvisMotionChart(PANEL, idvar="country", timevar="year")
plot(MC)

有人知道这是为什么吗?

1 个答案:

答案 0 :(得分:0)

您确实需要将数据从普通数据帧转换为面板数据帧。您可以使用数据框参数直接调用“ gvisMotionChart”。

请参见下面的代码:

library(WDI) # package for importing World Bank data
library(plm) # package for working with panel data
library(googleVis) # the googleVis package

# import the data using the WDI package
LONG <- WDI(
   country=c("AGO","BEN","BWA","BFA","BDI"), 
   indicator=c("SP.DYN.CBRT.IN", "SP.DYN.TFRT.IN", "SP.POP.TOTL", "NY.GDP.PCAP.KN"), 
   start=2005, 
   end=2009, 
   extra=FALSE)

# plot in a MotionChart using googleVis
MC <- gvisMotionChart(LONG, idvar="country", timevar="year")
plot(MC) 

输出: enter image description here