0

我想为如何解决 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)"))
4

1 回答 1

1

除了 set base_layer = ggplot(aes(...), ...),还需要指定一个 geom(几何对象)层,例如geom_point(aes(...))geom_rect(aes(...))geom_polygon(aes(...))附加到ggmap(...),以便将 base_layer 中使用的数据映射到视觉效果。

例如,

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

demo1-原始图例

我们还可以删除图例键中的背景灰色方块以获得更好的视觉效果(IMO),方法是附加:

+ theme(legend.key=element_blank())

注意:在您的示例中,lat应该lon交换 - 我们通常有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),当widthheight未指定时,它们将被计算为两个相邻点之间的最小间隙,即width = min(abs(diff(df$x)))height = min(abs(diff(df$y)))。在您的情况下,width = min(abs(diff(d$lon))) ~ 0.0001525275and height = min(abs(diff(d$lat))) ~ 3.292719e-05(由于runif,实际值可能会有所不同)。

鉴于示例或 GPS 坐标表示“原始点”而不是“每个区域的中心”,这种自动化将导致问题,因为两个相邻“原始点”之间的差距可能太窄而无法绘制像素。在这种情况下,我们将不得不手动指定widthandheight并记住瓷砖会重叠。

考虑您的示例,其中:

+ 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,请考虑以下简单示例:

# library(ggpubr)
# library(ggplot2)

df1 <- data.frame(
  x = c(2, 5, 7, 9),
  y = c(1, 2, 1,  2),
  z = factor(1:4)
)

df2 <- data.frame(
  x = c(2, 5, 5.1, 9),
  y = c(1, 2, 1  ,  2),
  z = factor(1:4)
)

df3 <- data.frame(
  x = c(2, 5  , 7, 9),
  y = c(1, 1.1, 1, 2),
  z = factor(1:4)
)

df4 <- data.frame(
  x = c(2, 5  , 5.1, 9),
  y = c(1, 1.1, 1  , 2),
  z = factor(1:4)
)

ggarrange(
  ggplot(df1, aes(x, y)) + geom_tile(aes(fill = z)),
  ggplot(df2, aes(x, y)) + geom_tile(aes(fill = z)),
  ggplot(df3, aes(x, y)) + geom_tile(aes(fill = z)),
  ggplot(df4, aes(x, y)) + geom_tile(aes(fill = z)),
  labels = paste0("df", 1:4),
  ncol = 2, 
  nrow = 2
)

因此,使用它geom_point来可视化“原始 GPS 点”会更简单。

在此处输入图像描述

于 2019-12-28T22:56:50.647 回答