I'm just starting off learning R and I'm attempting to create a map that shows the number of students in each town in Connecticut. I've got a .csv of location and enrollment info that looks like this:
我刚开始学习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
I've been able to generate the map that I'd like to overlay this information on using the following code:
我已经能够生成地图,我想用下面的代码覆盖这个信息:
tempMap <- get_openstreetmap(bbox = c(left = -73.8, bottom = 40.9, right = -71.7, top = 42.1), scale = 829486, color = "bw")
ggmap(tempMap)
I'm running in to a problem combining the two. Here is the code I'm using:
我遇到了一个把两者结合起来的问题。下面是我使用的代码:
enrollData<-read.csv("enrollData.csv")
enrollMap <- tempMap + geom_point(data = enrollData,aes(x = lon, y = lat, size = resStudent)
After this runs, I get an error message that reads:
运行完之后,我得到一条错误消息:
Error in Ops.raster(tempMap, geom_point(data = enrollData, aes(x = lon, :
Operator not meaningful for raster objects
Any ideas for how to fix this?
有办法解决这个问题吗?
1 个解决方案
#1
4
You need to call ggmap
first create the base layer. The following should work:
您需要首先调用ggmap来创建基层。以下工作:
ggmap(tempMap) + geom_point(data = enrollData,aes(x = lon, y = lat, size = resStudent)
#1
4
You need to call ggmap
first create the base layer. The following should work:
您需要首先调用ggmap来创建基层。以下工作:
ggmap(tempMap) + geom_point(data = enrollData,aes(x = lon, y = lat, size = resStudent)