如何将openstreetmap与R中的点组合

时间:2013-03-07 15:46:40

标签: r graphics ggplot2 ggmap

我刚开始学习R而我正在尝试创建一张地图,显示康涅狄格州每个城镇的学生人数。我有一个.csv的位置和注册信息,如下所示:

name       lon      lat resStudent
1   ANDOVER          -72.37472 41.73278        657
2   ANSONIA          -73.07900 41.34621       2999
3   ASHFORD          -72.12162 41.87307        736
4   AVON             -72.83052 41.80962       3563
5   BARKHAMSTED      -72.97222 41.92917        680
6   BEACON FALLS     -73.06176 41.44265       1039
7   BERLIN           -72.78064 41.61460       3165
8   BETHANY          -72.99250 41.42556       1028
9   BETHEL           -73.41396 41.37123       3034
10  BETHLEHEM        -73.20861 41.63917        490
11  BLOOMFIELD       -72.73336 41.83945       2484

我已经能够使用以下代码生成我想要覆盖此信息的地图:

tempMap <- get_openstreetmap(bbox = c(left = -73.8, bottom = 40.9, right = -71.7, top = 42.1), scale = 829486, color = "bw")
ggmap(tempMap)

我遇到了将两者结合起来的问题。这是我正在使用的代码:

enrollData<-read.csv("enrollData.csv")   
enrollMap <- tempMap + geom_point(data = enrollData,aes(x = lon, y = lat, size = resStudent)

运行后,我收到一条错误消息:

Error in Ops.raster(tempMap, geom_point(data = enrollData, aes(x = lon,  : 
  Operator not meaningful for raster objects

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:4)

您需要先致电ggmap创建基础层。以下应该有效:

ggmap(tempMap) + geom_point(data = enrollData,aes(x = lon, y = lat, size = resStudent)