Mapping postcodes in ggplot 2 using GADM

Hello,

I am new to R and have numerous attempts at this myself. I am stuck with what to do now.

I have a list of Australian postcodes that I would like to plot on a map of Australia for a scientific publication. I have managed to construct the map of Australia by doing this

library(maptools)
library(raster)
library(plyr)
library(ggplot2)
library(rgdal)

Australia<-getData("GADM", country="AU", level=0)
Australia1<-getData("GADM", country="AU", level=1)

plot(Australia)

And I got an outline of Australia

Then I did this

ggmap::geocode(paste(c(2001,2050,3123,2145),"Australia"))

#and got the following error message

Error in if (!override && (queries_used_in_last_second + queries_sought > :
missing value where TRUE/FALSE needed

Any help would be appreciated.

Kind Regards,

Karen

Hi Karen, it might be useful to confirm that the examples provided in the documentation work for you. That way you can be confident you have a healthy install of the relevant packages. It also gives you an opportunity to take some code that works and make small changes a bit at a time testing as you go that you still get expected results. In that spirit, here is what I copied from the documentation.

## Not run: requires Google API key, see ?register_google
## basic usage
########################################
# geocoding is most commonly used for addresses
geocode("1600 Amphitheatre Parkway, Mountain View, CA")
geocode("1600 Amphitheatre Parkway, Mountain View, CA", urlonly = TRUE)
# google can also geocode colloquial names of places
geocode("the white house")
# geocode can also accept character vectors of places
geocode(c("the white house", "washington dc"))

Thanks so much for your response.

I registered for an API but it isn't working in R from some reason. This is the code that I used

ggmap::register_google(key ="mykey")

It will not run for some reason. I just keep getting this error message.

Error in class(options) <- "ggmap_credentials" :
attempt to set an attribute on NULL.

I tried to use your examples but they didn't work either.

Kind Regards,

Karen

Karen, I edited your post as I don't want you accidentally sharing your key and letting someone else pretend to be you when accessing the Google map API

From what you say, it seems you've done step 1. Have you done step 2 in here ?

    1. Users must register with Google. You can do this at https://cloud.google.com/maps-platform/. While it will require a valid credit card (sorry!), there seems to be a fair bit of free use before you incur charges, and even then the charges are modest for light use.
  1. Users must enable the APIs they intend to use. What may appear to ggmap users as one overarching “Google Maps” product, Google in fact has several services that it provides as geo-related solutions. For example, the Maps Static API provides map images, while the Geocoding API provides geocoding and reverse geocoding services. Apart from the relevant Terms of Service, generally ggmap users don’t need to think about the different services. For example, you just need to remember that get_googlemap() gets maps, geocode() geocodes (with Google, DSK is done), etc., and ggmap handles the queries for you. However , you do need to enable the APIs before you use them. You’ll only need to do that once, and then they’ll be ready for you to use. Enabling the APIs just means clicking a few radio buttons on the Google Maps Platform web interface listed above, so it’s easy.
  2. Inside R, after loading the new version of ggmap , you’ll need provide ggmap with your API key, a hash value (think string of jibberish) that authenticates you to Google’s servers. This can be done on a temporary basis with register_google(key = "[your key]") or permanently using register_google(key = "[your key]", write = TRUE) (note: this will overwrite your ~/.Renviron file by replacing/adding the relevant line). If you use the former, know that you’ll need to re-do it every time you reset R.

Your API key is private and unique to you, so be careful not to share it online, for example in a GitHub issue or saving it in a shared R script file. If you share it inadvertantly, just get on Google’s website and regenerate your key - this will retire the old one. Keeping your key private is made a bit easier by ggmap scrubbing the key out of queries by default, so when URLs are shown in your console, they’ll look something like key=xxx . (Read the details section of the register_google() documentation for a bit more info on this point.)

Another thought. I didn't see a library call for ggmap, only that you call it's functions in this manner ggmap::
There was an issue on the GitHub for key stuff not working with the package not loaded. Is wouldn't hurt to try and load the package and try again.

Thanks so much for the suggestions. I am trying to do step 2. is this where I enable the radio buttons?

I am really working blind here but would like to learn.

Thanks for your help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.