Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...