17

I am trying to plot Google map that is queried using RgoogleMaps package and combine it with ggplot. Ultimately, I want to show total population using geom_point, somewhat similar to the picture below however I am trying to concentrate on Montgomery region because of over-plotting.

I am frustrated because I cannot plot my queried map in R. I tried a couple of packages such as read.jpeg and png but it didn't quite work out.

R codes:

#query google map
al1 <- GetMap(center=c(32.362563,-86.304474), zoom=11, destfile = "al2.jpeg", 
       format="jpg",maptype="roadmap")

#load only specific states
alabama <- subset(all_states, region %in% c("alabama"))

#population
p1 <- ggplot()
p1 <- p1 + geom_polygon(data=alabama, 
      aes(x=long, y=lat, group=group), colour="white", fill="grey10")
p1 <- p1 + geom_point(data=quote, aes(x=IntPtLon, y=IntPtLat, size=TotPop, 
      color=TotPop),colour="coral1") + scale_size(name="Total Pop")

enter image description here

EDIT:

Here is my rough result. I still want to:

  • Change the scale of dots' size because they seem rather small on the map.
  • Make dots transparent or not-filled so that the map is still visible.

enter image description here

al1 <- get_map(location = c(lon = -86.304474, lat = 32.362563), zoom = 11, maptype = 'terrain')
al1MAP <- ggmap(al1)+ geom_point(data=quote_mgm, aes(x=IntPtLon, y=IntPtLat, size=TotPop))
Andrie
  • 176,377
  • 47
  • 447
  • 496
Ken
  • 863
  • 3
  • 13
  • 24
  • 4
    you may wish to check out the ggmap and OpenStreetMap packages, both of which support ggplot2 raster plotting – Ian Fellows Apr 24 '12 at 17:48
  • 3
    Here's a nice example from the wiki that might be useful to check out: https://github.com/hadley/ggplot2/wiki/Crime-in-Downtown-Houston%2C-Texas-%3A-Combining-ggplot2-and-Google-Maps – John Colby Apr 24 '12 at 19:39
  • aha! I just found this website and got my answer. The downside is it does take some time to plot so I will check out ggmap and OSM. Thanks y'all! – Ken Apr 24 '12 at 19:55
  • 1
    if you show us the code you used to make the second example I'm sure someone will show you how to add transparency (using e.g. `alpha=0.5` *outside* the mapping definition, as you did with point colour above) and change the scaling of the points (see the `range` parameter in `?scale_size`) – Ben Bolker Apr 24 '12 at 21:26
  • Okay, adding `scale_area(range=c(0,25))` did the trick. Thank for the tip @BenBolker – Ken Apr 24 '12 at 21:37

1 Answers1

28

Is this what you're after. It uses the ggmap package, which simplifies the process. See ?get_map and ?ggmap for further options. An excellent resource is available in The R Journal

library(ggmap)
al1 = get_map(location = c(lon = -86.304474, lat = 32.362563), zoom = 11, maptype = 'roadmap')
al1MAP = ggmap(al1)
al1MAP

enter image description here

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
  • Yes, in addition to this, I want to plot dots on the top of this map (maybe using geom_point) but at the same time I want to make it visually appealing. I don't want my dots to hide/cover the existing map info. Any idea? – Ken Apr 24 '12 at 20:39
  • make the dots semi-transparent? – Ben Bolker Apr 24 '12 at 20:51
  • You can add additional layers to al1Map using regular ggplot2 geoms. So to add points, then something like `al1MAP + geom_point(data=data, aes(x=x, y=y))` will do. As for not wanting to cover existing map info - I can't help you there. Maybe you can try different map styles, to find something that contains less detail. – Sandy Muspratt Apr 24 '12 at 20:54
  • @Sandy, I don't think ggplot2 supports ggmap layers. I get the following error message: "ggplot2 doesn't know how to deal with data of class uneval" – Paul Rigor May 01 '13 at 06:28
  • 2
    @PaulRigor, It works for me. Create a data frame, just one point will do: `data = data.frame(x = -86.35, y = 32.4)` Add the point to al1MAP: `al1MAP + geom_point(data = data, aes(x = x, y = y), colour = "red", size = 5)` I get a big red dot in the river just to the west of highway 65. – Sandy Muspratt May 01 '13 at 07:41
  • @Sandy, you're right! The x, y variables of my data.frame didn't have the correct type. Works as described. Thanks! – Paul Rigor May 01 '13 at 08:54