简体   繁体   English

R:来自 ggmap 的 get_map()/get_googlemap() 错误

[英]R: Error in get_map()/get_googlemap() from ggmap

I am trying to use GGmap to create a plot of vehicle car crashes by state.我正在尝试使用 GGmap 按州创建车辆车祸图。 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 .特别是,我正在尝试重新创建此博客文章的可视化集群部分中显示的 usa-plot。

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

I have setup the Google API and see that it is recieving hits.我已经设置了 Google API 并看到它正在接收点击。 I have also enabled it and have the key.我也启用了它并拥有密钥。

In addition I have installed GGmap from the github account using this command:此外,我已经使用以下命令从 github 帐户安装了 GGmap:

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

since the CRAN one isn't updated.因为 CRAN 没有更新。

I have restarted and quit R several times as well but the error persists.我也多次重新启动并退出 R,但错误仍然存​​在。

Even if I just simply run:即使我只是简单地运行:

get_map()

it still results in the error:它仍然导致错误:

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))

I expect the map to output like this我希望在地图像输出

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.如果您需要更多信息,请告诉我。

Thank you.谢谢你。

This error is due to the google key not having the appropriate API activity enabled for that key.此错误是由于 google 密钥没有为该密钥启用适当的 API 活动。

Go into the google API console and enable the API "Maps Static API" and it should work for you.进入谷歌 API 控制台并启用 API“Maps Static API”,它应该适合你。

EDIT: Jan 2020 - I was doing some similar work and found that a similar API was failing because billing information had to be added to the project in the Google Cloud console before it would work.编辑:2020 年 1 月 - 我正在做一些类似的工作,发现一个类似的 API 失败了,因为必须将帐单信息添加到 Google Cloud 控制台中的项目中才能起作用。

有必要在Google API中确认您的信用卡,这样,您的API密钥就会被激活,您可以正常使用ggmap

Make sure to enable billing.确保启用计费。 You don't have to restrict api, but make sure all the api's you need are enabled.您不必限制 api,但请确保启用所有您需要的 api。 if you want to search location names, you'll need geocoding api in addition to static maps.如果您想搜索位置名称,除了静态地图之外,您还需要地理编码 API。 ggmap from CRAN is OK now (don't need github version).来自 CRAN 的 ggmap 现在可以了(不需要 github 版本)。

I am experiencing the same error and have followed through the instructions mentioned in the comment. 我遇到了同样的错误,并遵循了评论中提到的说明。 I added: 我补充说:

  • Static Maps API 静态地图API
  • Geolocation API 地理位置API
  • Geocoding API 地理编码API

I installed the ggmap from source with devtools::install_github("dkahle/ggmap") . 我用devtools::install_github("dkahle/ggmap")从源代码安装了ggmap I then registered my key with register_google(key = "xxx") confirmed that the key is available with has_google_key() . 然后,我使用register_google(key = "xxx")注册了我的密钥,确认该密钥可用于has_google_key()

Yet, I still receive when calling get_map 然而,我在调用get_map时仍然会收到

Error in aperm.default(map, c(2, 1, 3)) : invalid first argument, must be an array In addition: Warning message: In get_googlemap(center = location, zoom = zoom, filename = filename) : HTTP 400 Bad Request

I have also changed the source in get_map to "osm" without any success. 我还将get_map的源get_map为“osm”而没有任何成功。

Any further ideas to solve this are appreciated. 任何进一步的想法来解决这个问题,我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM