使用ggplot绘制谷歌地图R

问题描述:

我想绘制使用RgoogleMaps软件包查询并将其与ggplot结合使用的Google map。最终,我想通过使用geom_point来显示总人口,这与下面的图片有些类似,但是我正试图专注于蒙哥马利地区,因为过度阴谋。使用ggplot绘制谷歌地图R

我很沮丧,因为我不能在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)) 
+4

你不妨检查出ggmap和OpenStreetMap的包,这两个支持GGPLOT2光栅绘制 – 2012-04-24 17:48:50

+3

下面是来自维基一个很好的例子,可能是有用的退房:https://github.com/hadley/ggplot2/wiki/Crime-in-Downtown-Houston%2C-Texas-%3A-Combining-ggplot2-and-Google-Maps – 2012-04-24 19:39:23

+0

aha!我刚刚找到了这个网站,并得到了我的答案。缺点是它需要一些时间来绘制,所以我会检查出ggmap和OSM。谢谢你们! – Ken 2012-04-24 19:55:03

这是你以后。它使用ggmap包,这简化了过程。有关更多选项,请参阅?get_map?ggmap。一个优秀的资源可用在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

+0

是的,除此之外,我想绘制在这张地图顶部的点(也许使用geom_point),但同时我想让它看起来很吸引人。我不想让自己的点隐藏/覆盖现有的地图信息。任何想法? – Ken 2012-04-24 20:39:27

+0

使点半透明? – 2012-04-24 20:51:13

+0

您可以使用常规ggplot2几何图形将其他图层添加到al1Map。所以要添加点,那么像'al1MAP + geom_point(data = data,aes(x = x,y = y))'就可以。至于不想覆盖现有的地图信息 - 我帮不了你。也许你可以尝试不同的地图样式,以找到包含较少细节的东西。 – 2012-04-24 20:54:11