R:ggmap中的get_map()/get_googlemap()错误 [英] R: Error in get_map()/get_googlemap() from ggmap

查看:483
本文介绍了R:ggmap中的get_map()/get_googlemap()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GGmap来按州创建车祸图.该地图将包含根据该州发生的车祸次数确定大小的点.

I am trying to use GGmap to create a plot of vehicle car crashes by state. The map will have dots which are sized based on the number of car crashes in the state.

特别是,我试图重新创建此博客文章.

In particular I am trying to recreate the usa-plot shown in the visualizing clusters section of this blog post.

但是,每当我尝试创建地图时,都会出现此错误.

However, whenever I try to create the map I get this error.

Error in aperm.default(map, c(2, 1, 3)) : 
  invalid first argument, must be an array

我已经设置了Google API,并看到它收到了匹配.我也启用了它并拥有密钥.

I have setup the Google API and see that it is recieving hits. I have also enabled it and have the key.

此外,我还使用以下命令从github帐户安装了GGmap:

In addition I have installed GGmap from the github account using this command:

devtools::install_github("dkahle/ggmap", ref = "tidyup", force=TRUE)

因为CRAN尚未更新.

since the CRAN one isn't updated.

我也重新启动并退出了R次,但错误仍然存​​在.

I have restarted and quit R several times as well but the error persists.

即使我只是简单地运行:

Even if I just simply run:

get_map()

它仍然导致错误:

Error in aperm.default(map, c(2, 1, 3)) : 
      invalid first argument, must be an array

下面是我的代码,它类似于博客文章中的代码:

Below is my code, it is similar to the code in the blog post:

mydata$State <- as.character(mydata$State)
mydata$MV.Number = as.numeric(mydata$MV.Number)
mydata = mydata[mydata$State != "Alaska", ]
mydata = mydata[mydata$State != "Hawaii", ]
devtools::install_github("dkahle/ggmap", ref = "tidyup", force=TRUE)
library(ggmap)
ggmap::register_google(key = "...") #my key is here
for (i in 1:nrow(mydata)) {
  latlon = geocode(mydata[i,1])
  mydata$lon[i] = as.numeric(latlon[1])
  mydata$lat[i] = as.numeric(latlon[2])
}
mv_num_collisions = data.frame(mydata$MV.Number, mydata$lon, mydata$lat)

colnames(mv_num_collisions) = c('collisions','lon','lat')
usa_center = as.numeric(geocode("United States"))


USAMap = ggmap(get_googlemap(center=usa_center, scale=2, zoom=4), 
extent="normal")
USAMap + 
   geom_point(aes(x=lon, y=lat), data=mv_num_collisions, col="orange", 
alpha=0.4, size=mv_num_collisions$collisions*circle_scale_amt) +  
   scale_size_continuous(range=range(mv_num_collisions$collisions))

我希望地图输出为

但是我似乎无法通过此错误.

But I cannot seem to get passed this error.

如果有人可以帮助,那就太好了.

If anyone can help that would be great.

如果您需要更多信息,请告诉我.

Please let me know if you need any more information.

谢谢.

推荐答案

此错误是由于Google密钥未为该密钥启用适当的API活动.

This error is due to the google key not having the appropriate API activity enabled for that key.

进入google API控制台并启用API"Maps Static API",它应该可以为您工作.

Go into the google API console and enable the API "Maps Static API" and it should work for you.

这篇关于R:ggmap中的get_map()/get_googlemap()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆