ggmap中未显示base_layer

时间:2019-12-28 18:21:02

标签: r ggplot2 leaflet ggmap stamen-maps

我想就如何解决ggmap问题提供任何建议。

让我们假设我们有一些空间模型和残差,然后将其绘制在地图上。 使用ggmap函数时,我可以看到基线背景图并为base_layer - fill倾斜,但是在图中看不到。

我提供了可复制的示例:

library(ggmap)
library(maptools)
library(ggplot2)


#map background
bboxPrague <- c(14.22,49.94,14.71,50.18)
ggMapPrague <- get_map(location = bboxPrague, source = "stamen",maptype = "toner", crop = TRUE, zoom = 12)
ggmap(ggMapPrague)

d = data.frame(
  pred_res = runif(2000, -50, 50),
  lon = runif(2000, 49.94, 50.18),
  lat = runif(2000, 14.22, 14.71)

)
d

#top&bottom coding and discreting pred_res....8
d$res_coded<-replace(d$pred_res,d$pred_res<(-1),8)
d$res_coded<-replace(d$res_coded,d$pred_res>=-1,7)
d$res_coded<-replace(d$res_coded,d$pred_res>=-0.4,6)
d$res_coded<-replace(d$res_coded,d$pred_res>=-0.1,5)
d$res_coded<-replace(d$res_coded,d$pred_res>=0,4)
d$res_coded<-replace(d$res_coded,d$pred_res>=0.1,3)
d$res_coded<-replace(d$res_coded,d$pred_res>=0.4,2)
d$res_coded<-replace(d$res_coded,d$pred_res>=1,1)

d %>% head

d$res_coded %>% head

d$res_coded = as.factor(d$res_coded)

ggmap(ggMapPrague, base_layer = ggplot(d, aes(x = lat, y = lon, fill = res_coded)),extent="device",legend = "topleft") + 
  geom_tile(alpha=0.5) +
  scale_fill_brewer(palette="RdYlGn",name="Residual",labels = c("[1;+inf)","[0.4;1)","[0.1;0.4)","[0;0.1)","[-0.1;0)","[-0.4;-0.1)","[-1;-0.4)","(-inf;-1)"))

1 个答案:

答案 0 :(得分:1)

除了设置base_layer = ggplot(aes(...), ...)之外,还需要指定一个geom_point(aes(...))geom_rect(aes(...))geom_polygon(aes(...))之类的几何层(附加到{{ 1}},以便将base_layer中使用的数据映射到视觉效果。

例如,

ggmap(...)

demo1-orignal-legend

我们还可以通过添加以下内容来删除图例键中的灰色背景方块,以提供更好的视觉效果(IMO):

+ geom_point(aes(x = lon, y = lat, color = res_coded)) +

注意:在您的示例中,+ theme(legend.key=element_blank()) lat应该互换-我们通常有lon。请参阅以下演示代码中的修复程序。

演示

demo-full

x = lon, y = lat

问题library(ggmap) library(maptools) library(ggplot2) # map background # bbox = c( left = min(lon), bottom = min(lat), right = max(lon), top = max(lat) ) bboxPrague <- c(14.22,49.94,14.71,50.18) ggMapPrague <- get_map(location = bboxPrague, source = "stamen",maptype = "toner", crop = TRUE, zoom = 12) ggmap(ggMapPrague) d = data.frame( pred_res = runif(2000, -50, 50), lat = runif(2000, 49.94, 50.18), lon = runif(2000, 14.22, 14.71) ) d #top&bottom coding and discreting pred_res....8 d$res_coded<-replace(d$pred_res,d$pred_res<(-1),8) d$res_coded<-replace(d$res_coded,d$pred_res>=-1,7) d$res_coded<-replace(d$res_coded,d$pred_res>=-0.4,6) d$res_coded<-replace(d$res_coded,d$pred_res>=-0.1,5) d$res_coded<-replace(d$res_coded,d$pred_res>=0,4) d$res_coded<-replace(d$res_coded,d$pred_res>=0.1,3) d$res_coded<-replace(d$res_coded,d$pred_res>=0.4,2) d$res_coded<-replace(d$res_coded,d$pred_res>=1,1) d %>% head d$res_coded %>% head d$res_coded = as.factor(d$res_coded) ggmap(ggMapPrague, base_layer = ggplot(d, aes(x = lon, y = lat)), extent="device", legend = "topleft") + geom_point(aes(x = lon, y = lat, color = res_coded)) + scale_fill_brewer( palette="RdYlGn", name="Residual", labels = c("[1;+inf)","[0.4;1)","[0.1;0.4)", "[0;0.1)","[-0.1;0)","[-0.4;-0.1)", "[-1;-0.4)","(-inf;-1)")) + theme(legend.key=element_blank())

geom_tile绘制由geom_tile指定的矩形,当未指定(center_x, center_y, width, height)width时,它们将被计算为两个相邻点之间的最小间隙,即{ {1}}和height。根据您的情况,width = min(abs(diff(df$x)))height = min(abs(diff(df$y)))(由于width = min(abs(diff(d$lon))) ~ 0.0001525275,实际值可能会有所不同)。

给定示例或GPS坐标表示“原始点”而不是“每个区域的中心”,这种自动操作将引起问题,因为两个相邻“原始点”之间的间隙可能太窄而无法绘制一个像素。在这种情况下,我们将必须手动指定height = min(abs(diff(d$lat))) ~ 3.292719e-05runif,并记住图块会重叠。

请考虑您的示例,

width

enter image description here

要进一步说明height的自动宽度和高度,请考虑以下简单示例:

+ geom_tile(aes(fill=res_coded, height = 3E-3, width=3E-3))

# note that when `height` and `width` is small enough, 
# for example, 
#    `height = 3E-4, width=3E-3` will show horizontal lines along `width`
#    `height = 3E-3, width=3E-4` will show vertical lines along `height`
#    `height = 3E-4, width=3E-4` will show nothing (which is your case)

因此,使用geom_tile可视化“原始GPS点”将更加简单。

enter image description here