用R中的ggplot绘制谷歌地图

时间:2012-04-24 16:38:41

标签: r google-maps ggplot2 geocoding ggmap

我正在尝试绘制使用RgoogleMaps包查询的Google map并将其与ggplot结合使用。最后,我想使用geom_point显示总人口,有点类似于下图,但是由于过度绘图,我试图集中在蒙哥马利地区。

我感到很沮丧,因为我无法在R中绘制我查询过的地图。我尝试了几个包,例如read.jpegpng但是它没有完全解决。

R代码:

#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

修改

这是我的粗略结果。我还是想:

  • 更改点的大小,因为它们在地图上看起来相当小。
  • 使点透明或未填充,以便地图仍然可见。

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))

1 个答案:

答案 0 :(得分:27)

这就是你所追求的。它使用ggmap包,简化了流程。有关更多选项,请参阅?get_map?ggmapThe 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