10

I can plot the state of Louisiana just fine...

require(ggplot2)
require(ggmap)
require(maps)
LA <- map_data("state", region="louisiana")
ggplot(LA, aes(x=long, y=lat))+geom_polygon()

enter image description here

Now, I have data on how many sales calls were made to particular cities in LA. How would I add a point for each city where a sales call was made to the plot?

salesCalls <- data.frame(State=rep("louisiana",5), 
                             City=c("Baton Rouge", "New Orleans", "Shreveport", "Lafayette", "Mandeville"),
                             Calls=c(10,5,8,13,2))
salesCalls
      State        City Calls
1 louisiana Baton Rouge    10
2 louisiana New Orleans     5
3 louisiana  Shreveport     8
4 louisiana   Lafayette    13
5 louisiana  Mandeville     2
Ben
  • 20,038
  • 30
  • 112
  • 189

2 Answers2

15
require(ggplot2)
require(ggmap)
require(maps)
LA <- map_data("state", region="louisiana")

salesCalls <- data.frame(State=rep("louisiana",5), 
                         City=c("Baton Rouge", "New Orleans", "Shreveport", 
                                "Lafayette", "Mandeville"),
                         Calls=c(10,5,8,13,2))

salesCalls <- cbind(geocode(as.character(salesCalls$City)), salesCalls)

salesCalls
#         lon      lat     State        City Calls
# 1 -91.14032 30.45828 louisiana Baton Rouge    10
# 2 -90.07153 29.95107 louisiana New Orleans     5
# 3 -93.75018 32.52515 louisiana  Shreveport     8
# 4 -92.01984 30.22409 louisiana   Lafayette    13
# 5 -90.06563 30.35825 louisiana  Mandeville     2

ggplot(LA, aes(x=long, y=lat)) +
  geom_polygon() +
  coord_map() +
  geom_point(data=salesCalls, aes(x=lon, y=lat, size=Calls), color="orange")

ggplot2

On a Google Map:

ggmap(get_map(location = 'Louisiana', zoom = 7)) +
  geom_point(data=salesCalls, aes(x=lon, y=lat, size=Calls), color="orange")

Google Map

JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • Thanks a ton. I'm really interested in getting that google map to work now, but when I execute your code all I get is the grey ggplot background with orange points (where they're supposed to be). In other words, there's no map. Any idea why? – Ben Mar 13 '15 at 20:10
  • @Ben Try running `get_map(location = 'Louisiana', zoom = 7)` and see what comes back. Are you behind a proxy server? I also noticed a typo in my code, doubtful that would be it, but double-check the spelling of "Louisiana" – JasonAizkalns Mar 13 '15 at 20:24
  • Never mind - I think this issue goes a little deeper than I initially expected. – Ben Mar 13 '15 at 20:24
  • @Ben [This Article](http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf) is a great guide to getting started with `library(ggmap)` – JasonAizkalns Mar 13 '15 at 20:25
  • When I run `get_map(location = 'Louisiana', zoom = 7)` I get the message "Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Louisiana&zoom=7&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Louisiana&sensor=false" along with a big matrix of values – Ben Mar 13 '15 at 20:27
  • @Ben What about when running `ggmap(get_map(location = 'Louisiana', zoom = 7))` – JasonAizkalns Mar 14 '15 at 17:08
  • No luck. Still just a blank grey screen. – Ben Mar 16 '15 at 15:39
  • I fixed the example above with hardcoded lon-lat coordinates for the dataset and for the `get_map` thingy. If it does not get accepted here is this comment :) – Ufos May 29 '18 at 19:07
  • I get "1: geocode failed with status OVER_QUERY_LIMIT, location = "Baton Rouge" , solution is to use option source= "dsk" for geocode. – Guangbo Chen Sep 26 '18 at 18:24
0

The following script should work for the google maps part:

ggmap(get_map(location = 'Louisiana', zoom = 7)) +
  geom_point(
    data = salesCalls, 
    aes(x = salesCalls$lon, y = salesCalls$lat, size = salesCalls$Calls), 
    color = "orange"
  )
Stephan
  • 2,056
  • 1
  • 9
  • 20
NeilC
  • 33
  • 2