1

我可以用点覆盖街道地图,用点覆盖等值线地图,但不能将这三者结合起来。我怀疑我不能同时打印两个 ggmap 对象。关于如何做到这一点的任何想法?我的目标是让底层由街道(以及可选的名称)、人口密度的中间层和点的顶层组成。

谢谢。代码如下。

#load libraries
library(choroplethr)
library(choroplethrZip)
library(ggplot2)
library(ggmap)

#data csgb consists of lat col, long col, and key col
csgb
LATITUDE LONGITUDE BRANCH_KEY
1  32.47220 -85.00600       4734
2  32.53800 -84.86872       2594
3  32.46230 -84.92930       2604
4  32.47980 -84.94440       2584
5  32.36980 -84.96440       4727
6  32.49530 -85.02460       2948
7  32.37880 -84.90680       2601
8  32.52820 -84.99200       2632
9  32.49200 -85.01850       2947
10 32.52820 -84.99200       2595
11 32.53950 -84.96621       2589
12 32.42834 -84.94377       2583
13 32.46171 -84.97530       2578
14 32.54730 -84.93620       2603
15 32.52391 -84.92921       2598
16 32.79890 -84.57980       2588
17 32.46736 -84.99550       2907
18 32.47630 -84.99170       2602
19 32.47850 -84.97950       2590
20 32.46480 -84.98760       2580
21 32.52365 -84.92954       2586

#choropleth map is a ggplot object (and it renders on a device)
map <- zip_choropleth(df_pop_zip, title = 'Columbus - Phenix Cty Zip Codes',     legend = 'Population Key', 
     num_colors = 9, 
     msa_zoom = 'Columbus, GA-AL')
class(map)
[1] "gg"     "ggplot"

#get a bounding box for a street map
leftbbx = min(map$data$long)
bottombbx = min(map$data$lat)
rightbbx = max(map$data$long)
topbbx = max(map$data$lat)v

# street map is a ggplot object, and it displays on a device
m1 <- get_map(location = c(leftbbx, bottombbx, rightbbx, topbbx), source = 'google', maptype = 'roadmap')
m1 <- ggmap(m1)
class(m1)
[1] "gg"     "ggplot"

#get points
mapoints <- geom_point(aes(x = LONGITUDE, y = LATITUDE, group = BRANCH_KEY), data = csgb, color = 'red')

# this works! ;-)
map + mapoints

# this also works! ;-)
m1 + mapoints

# this fails? :-(
map + m1
Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "Ops.data.frame") for "+" 
m1 + map
Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "Ops.data.frame") for "+" 
4

0 回答 0