简体   繁体   中英

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

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

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

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

since the CRAN one isn't updated.

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

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.

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

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.

有必要在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. if you want to search location names, you'll need geocoding api in addition to static maps. ggmap from CRAN is OK now (don't need github version).

I am experiencing the same error and have followed through the instructions mentioned in the comment. I added:

  • Static Maps API
  • Geolocation API
  • Geocoding API

I installed the ggmap from source with devtools::install_github("dkahle/ggmap") . I then registered my key with register_google(key = "xxx") confirmed that the key is available with has_google_key() .

Yet, I still receive when calling 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.

Any further ideas to solve this are appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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