Plotting google map with ggplot in R
Asked Answered
S

1

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))
Suspiration answered 24/4, 2012 at 16:38 Comment(5)
you may wish to check out the ggmap and OpenStreetMap packages, both of which support ggplot2 raster plottingLori
Here's a nice example from the wiki that might be useful to check out: github.com/hadley/ggplot2/wiki/…Genic
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!Suspiration
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)Barraza
Okay, adding scale_area(range=c(0,25)) did the trick. Thank for the tip @BenBolkerSuspiration
B
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

Bur answered 24/4, 2012 at 20:16 Comment(6)
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?Suspiration
make the dots semi-transparent?Barraza
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.Bur
@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"Carpophagous
@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.Bur
@Sandy, you're right! The x, y variables of my data.frame didn't have the correct type. Works as described. Thanks!Carpophagous

© 2022 - 2024 — McMap. All rights reserved.