1

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

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)

有人知道这是为什么吗?

4

1 回答 1

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) 

输出: 在此处输入图像描述

于 2020-03-04T15:03:12.633 回答