dkahle / ggmap

A package for plotting maps in R with ggplot2
764 stars 231 forks source link

GGMap error with aperm.default #262

Closed SrikarMurali closed 4 years ago

SrikarMurali commented 5 years ago

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.

dkahle commented 5 years ago

Hi @SrikarMurali – Thanks for the report. I was getting this error in dev a few days ago, but not getting it now. Can you re-install (devtools::install_github("dkahle/ggmap")), reload, and try again, please? Thanks!

JamesKondilios commented 5 years ago

Hey @dkahle,

Im also getting the same error as our friend above. I have re-installed from GitHub, and when after running example code: p <- ggmap(get_googlemap( c(-122.335167,47.608013), zoom = 11, scale = 2, maptype ='terrain',color = 'color'))

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

Im not sure how this would be debugged, but any help would be greatly appreciated.

Thanks, James

dkahle commented 5 years ago

Hi @JamesKondilios – Thanks for the report. I can't seem to reproduce this error. I would guess that, for some reason, you don't have the most recent version of ggmap. Try re-installing with devtools::install_github("dkahle/ggmap"), rebooting R and reloading ggmap and having another go. Touch base if it doesn't work for you! Perhaps a devtools::session_info() would help us clue in further.

JamesKondilios commented 5 years ago

Hey @dkahle,

No worries mate, cheers for getting back to me so promptly.

My session info:

> devtools::session_info()
─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.5.2 (2018-12-20)
 os       macOS High Sierra 10.13.2   
 system   x86_64, darwin15.6.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_AU.UTF-8                 
 ctype    en_AU.UTF-8                 
 tz       Australia/Sydney            
 date     2019-02-04                  

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date       lib source                       
 assertthat    0.2.0   2017-04-11 [1] CRAN (R 3.5.0)               
 backports     1.1.3   2018-12-14 [1] CRAN (R 3.5.0)               
 bindr         0.1.1   2018-03-13 [1] CRAN (R 3.5.0)               
 bindrcpp      0.2.2   2018-03-29 [1] CRAN (R 3.5.0)               
 bitops        1.0-6   2013-08-17 [1] CRAN (R 3.5.0)               
 callr         3.1.1   2018-12-21 [1] CRAN (R 3.5.0)               
 cli           1.0.1   2018-09-25 [1] CRAN (R 3.5.0)               
 colorspace    1.4-0   2019-01-13 [1] CRAN (R 3.5.2)               
 crayon        1.3.4   2017-09-16 [1] CRAN (R 3.5.0)               
 curl          3.3     2019-01-10 [1] CRAN (R 3.5.2)               
 desc          1.2.0   2018-05-01 [1] CRAN (R 3.5.0)               
 devtools    * 2.0.1   2018-10-26 [1] CRAN (R 3.5.2)               
 digest        0.6.18  2018-10-10 [1] CRAN (R 3.5.0)               
 dplyr         0.7.8   2018-11-10 [1] CRAN (R 3.5.0)               
 fs            1.2.6   2018-08-23 [1] CRAN (R 3.5.0)               
 ggmap       * 3.0.0   2019-02-04 [1] Github (dkahle/ggmap@fc5e0fd)
 ggplot2     * 3.1.0   2018-10-25 [1] CRAN (R 3.5.0)               
 glue          1.3.0   2018-07-17 [1] CRAN (R 3.5.0)               
 gtable        0.2.0   2016-02-26 [1] CRAN (R 3.5.0)               
 httr          1.4.0   2018-12-11 [1] CRAN (R 3.5.0)               
 jpeg          0.1-8   2014-01-23 [1] CRAN (R 3.5.0)               
 labeling      0.3     2014-08-23 [1] CRAN (R 3.5.0)               
 lazyeval      0.2.1   2017-10-29 [1] CRAN (R 3.5.0)               
 magrittr      1.5     2014-11-22 [1] CRAN (R 3.5.0)               
 memoise       1.1.0   2017-04-21 [1] CRAN (R 3.5.0)               
 munsell       0.5.0   2018-06-12 [1] CRAN (R 3.5.0)               
 pillar        1.3.1   2018-12-15 [1] CRAN (R 3.5.0)               
 pkgbuild      1.0.2   2018-10-16 [1] CRAN (R 3.5.0)               
 pkgconfig     2.0.2   2018-08-16 [1] CRAN (R 3.5.0)               
 pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.5.0)               
 plyr          1.8.4   2016-06-08 [1] CRAN (R 3.5.0)               
 png           0.1-7   2013-12-03 [1] CRAN (R 3.5.0)               
 prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.5.0)               
 processx      3.2.1   2018-12-05 [1] CRAN (R 3.5.0)               
 ps            1.3.0   2018-12-21 [1] CRAN (R 3.5.0)               
 purrr         0.3.0   2019-01-27 [1] CRAN (R 3.5.2)               
 R6            2.3.0   2018-10-04 [1] CRAN (R 3.5.0)               
 Rcpp          1.0.0   2018-11-07 [1] CRAN (R 3.5.0)               
 remotes       2.0.2   2018-10-30 [1] CRAN (R 3.5.0)               
 RgoogleMaps   1.4.3   2018-11-07 [1] CRAN (R 3.5.0)               
 rjson         0.2.20  2018-06-08 [1] CRAN (R 3.5.0)               
 rlang         0.3.1   2019-01-08 [1] CRAN (R 3.5.2)               
 rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.5.0)               
 rstudioapi    0.9.0   2019-01-09 [1] CRAN (R 3.5.2)               
 scales        1.0.0   2018-08-09 [1] CRAN (R 3.5.0)               
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.0)               
 stringi       1.2.4   2018-07-20 [1] CRAN (R 3.5.0)               
 stringr       1.3.1   2018-05-10 [1] CRAN (R 3.5.0)               
 tibble        2.0.1   2019-01-12 [1] CRAN (R 3.5.2)               
 tidyr         0.8.2   2018-10-28 [1] CRAN (R 3.5.0)               
 tidyselect    0.2.5   2018-10-11 [1] CRAN (R 3.5.0)               
 usethis     * 1.4.0   2018-08-14 [1] CRAN (R 3.5.0)               
 withr         2.1.2   2018-03-15 [1] CRAN (R 3.5.0)               

[1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library

I reinstalled ggmaps from git and got the same error. I decided to run a more simple example:

ggmap::register_google(key = "Please_steal_my_API_key")
get_googlemap("waco texas", zoom = 12) %>% ggmap()

and I got the following error: Source : https://maps.googleapis.com/maps/api/staticmap?center=waco%20texas&zoom=12&size=640x640&scale=2&maptype=terrain&key=XXX HTTP 400 Bad RequestError in aperm.default(map, c(2, 1, 3)) : invalid first argument, must be an array

Thanks, James

dkahle commented 5 years ago

Hi @JamesKondilios – The HTTP 400 error is indicating that something went wrong. When I manually replace my API with your XXX in the printed line above, I get the proper map in a browser, do you?

JamesKondilios commented 5 years ago

I did not get the map in a browser. I just realised that I didn't enable the static maps API on google cloud ... after doing such, all problems were solved.

Thank you so much for your help. Your package and debugging is great!

dkahle commented 5 years ago

Great, happy you're back up and running! Thanks for your kind words; let me know if I can help in the future.

rachaelantwis commented 5 years ago

Hi all, I'm struggling with the same error. How do I enable the static maps on google cloud? Many thanks in advance

eglanterina commented 5 years ago

I am following a tutorial using the following code. I am receiving the same error and have static maps enabled (or I believe I do...).

library(ggmap) library(mapproj) map <- get_map(location = 'Europe', zoom = 4) ggmap(map)

image

radhikesh commented 5 years ago

I was having the same issue but it worked in the end.

I installed the latest version using devtools::install_github("dkahle/ggmap") . I checked if my Maps Static API is enabled or not, it was enabled but it was showing me a warning sign, the warning was about to restrict my API key. I restricted the API key and it worked.

Please see below screen shot of restricting API key.

image

rachaelantwis commented 5 years ago

Thank you. I'll see if that works!

meixilin commented 5 years ago

Hi, for those of you still have problem after:

  1. enable API
  2. restrict API key

See if you used the cloud platform before but not enabled billing yet

After enable billing my problem went away. Thank you for this great package!

benscarlson commented 4 years ago

I ran into this same problem but found a different fix. It seems that my "free trial" was over and I needed to upgrade. Once I did this the API calls were successful. If you go to https://console.cloud.google.com/google/maps-apis you might see a header at the top that looks like this:

image

Just click on "UPGRADE" and the API calls will work again.

This means you could potentially be billed for API requests, but it looks like there is no (or extremely low) costs for personal usage. https://developers.google.com/maps/documentation/maps-static/usage-and-billing

$2 for 1000 requests, and there might be a $200 credit applied each month, so hopefully the bill is $0 each month!

kent37 commented 4 years ago

The aperm error is seen when the map request returns an error and no map, for example if the API key is not valid. The problem is at https://github.com/dkahle/ggmap/blob/master/R/get_googlemap.R#L372. When an error status is received, the code gives a warning but then continues to try to process the map, causing a hard error at https://github.com/dkahle/ggmap/blob/master/R/get_googlemap.R#L391p

@dkahle converting the error to a warning looks like a bad idea, better to let the error stop the processing with a clearer message.

mayank634 commented 4 years ago

I am getting the same error as the OP. I have tried doing the following

I am still getting this error

Source : https://maps.googleapis.com/maps/api/staticmap?center=texas&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
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, maptype = maptype,  :
  HTTP 400 Bad Request

My session info

R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggmap_3.0.0   ggplot2_3.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1          pillar_1.4.1        compiler_3.6.0      plyr_1.8.5          bitops_1.0-6        tools_3.6.0        
 [7] packrat_0.5.0       lifecycle_0.1.0     tibble_2.1.2        gtable_0.3.0        lattice_0.20-38     pkgconfig_2.0.2    
[13] png_0.1-7           rlang_0.4.0         rstudioapi_0.10     curl_3.3            withr_2.1.2         dplyr_0.8.1        
[19] httr_1.4.0          stringr_1.4.0       RgoogleMaps_1.4.5.3 grid_3.6.0          tidyselect_0.2.5    glue_1.3.1         
[25] R6_2.4.0            jpeg_0.1-8.1        sp_1.3-1            purrr_0.3.2         tidyr_0.8.3         magrittr_1.5       
[31] scales_1.1.0        assertthat_0.2.1    colorspace_1.4-1    stringi_1.4.3       lazyeval_0.2.2      munsell_0.5.0      
[37] crayon_1.3.4        rjson_0.2.20
yasetevo commented 4 years ago

Hello,

I was getting the same error. I took the following steps:

  1. In Google Maps - API I enabled Maps Static API
  2. In Restrict and rename API Key, for my key I enable restriction and API Restrictions Type of Filter -> I selected Maps Static API
  3. Enable Billing.

I hope that works.

Thank you,

Evo

saurabhheda commented 4 years ago

Any workaround? I am still facing this issue. Tried all the options.

saurabhheda commented 4 years ago

The aperm error is seen when the map request returns an error and no map, for example if the API key is not valid. The problem is at https://github.com/dkahle/ggmap/blob/master/R/get_googlemap.R#L372. When an error status is received, the code gives a warning but then continues to try to process the map, causing a hard error at https://github.com/dkahle/ggmap/blob/master/R/get_googlemap.R#L391p

@dkahle converting the error to a warning looks like a bad idea, better to let the error stop the processing with a clearer message.

Something to do with the get_map function?

ghost commented 4 years ago

I was getting this error because I'd accidentally included some whitespace at the start of my key.

It was confusing because I could use the url displayed in the error message:

https://maps.googleapis.com/maps/api/staticmap??center=portsmouth,%20uk&zoom=9&size=640x640&scale=2&maptype=roadmap&language=en-EN&key= some_kind_of_key HTTP 400 Bad Request

To generate a static map 'manually' in my web browser. This was because the space at the start of my string was automatically coerced to "%20".

Could/should get_map() / register_google() include functionality for coercing strings to HTML?

kent37 commented 4 years ago

@dkahle Please consider re-opening this issue and changing the error handling as I suggested above. Clearly this is still an issue that people are struggling with. I'm happy to make a pull request if you would like.

saurabhheda commented 4 years ago

@kent37 Thanks, Is there any work-around ?

@dkahle Please any input from your side

kent37 commented 4 years ago

@saurabhheda the problem is with your access to Google maps, you need to fix your API key or account. There are several suggestions in this issue.

dkahle commented 4 years ago

Hi @kent37 – Sorry for my delayed response, I just don't have much time to work on this. Can you submit a PR, please? Many thanks.

nahin29 commented 4 years ago

I also faced problem today. How I will fix it @dkahle

xeniaserrat commented 4 years ago

I was facing the same problems, followed the suggestions above and kept getting the error message until I realized I included the square brackets when specifying register_google(key = "[your key]"). I simply removed the square brackets and it works now!

jmhulbert commented 4 years ago

I had this issue and had to remove write= TRUE from register_google() to use get_map().

ahcyip commented 4 years ago

never mind - i now understand the purpose of xxx (to scrub the key so that it's not visible when copying and pasting)

get_googlemap("waco,texas", maptype = "hybrid") %>% ggmap() Source : https://maps.googleapis.com/maps/api/staticmap?center=waco,texas&zoom=10&size=640x640&scale=2&maptype=hybrid&key=xxx-second part of API key Error in aperm.default(map, c(2, 1, 3)) : invalid first argument, must be an array In addition: Warning message: In get_googlemap("waco,texas", maptype = "hybrid") : HTTP 400 Bad Request

Could this problem be how the API key gets put in? I'm not sure why "xxx" is still in the key field.

Waikat0 commented 3 years ago

Hey, I had sort of the same problem. This is how I solved it: in addition to Direction API, I activated Static Maps, Map Embed, Geolocation and Geocoding. Works perfectly fine now. Static maps might also be sufficient.

doughnutnz commented 3 years ago

I had a similar issue and it turned out to be that I had not enabled billing for my API key/Project

sanjmeh commented 2 years ago

I got the error again and it seems it is neither related to API nor billing - both were enabled and still I got the error repeatedly on running ggmap(). I removed API restrictions, and it started working. This could be the cause but wait. The problem came again when the map coordinates were landing in the middle of an ocean resulting in no land. This is not repeatable also, as sometimes it goes through and shows a blue ocean while other times it aborts with the same error.

So net net it seems google API returns the same error in many cases and is not consistent. It will be nice if the package authors can trap (trycatch) this error and spit some meaningful error messages... as it is really frustrating when the error messages have no meaning... it cost me several hours today.