base_layer ggmap 中未显示
base_layer not showing in ggmap
我想向 k 寻求有关如何解决 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)"))
除了设置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)) +
我们还可以删除图例键中的背景灰色方块以获得更好的视觉效果(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)
指定的矩形,当width
和height
不指定时,计算为相邻两点之间的最小间距,即, width = min(abs(diff(df$x)))
和 height = min(abs(diff(df$y)))
。在您的情况下,width = min(abs(diff(d$lon))) ~ 0.0001525275
和 height = min(abs(diff(d$lat))) ~ 3.292719e-05
(由于 runif
,实际值可能略有不同)。
这种自动化会导致问题,例如 GPS 坐标表示 "raw points" 而不是 "center of each region",因为两个相邻 "raw points" 之间的距离可能太窄,不值得要绘制的像素。在这种情况下,我们必须手动指定 width
和 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
的自动宽度和高度,请考虑以下简单示例:
# 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
可视化"raw GPS points"会更简单。
我想向 k 寻求有关如何解决 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)"))
除了设置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)) +
我们还可以删除图例键中的背景灰色方块以获得更好的视觉效果(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)
指定的矩形,当width
和height
不指定时,计算为相邻两点之间的最小间距,即, width = min(abs(diff(df$x)))
和 height = min(abs(diff(df$y)))
。在您的情况下,width = min(abs(diff(d$lon))) ~ 0.0001525275
和 height = min(abs(diff(d$lat))) ~ 3.292719e-05
(由于 runif
,实际值可能略有不同)。
这种自动化会导致问题,例如 GPS 坐标表示 "raw points" 而不是 "center of each region",因为两个相邻 "raw points" 之间的距离可能太窄,不值得要绘制的像素。在这种情况下,我们必须手动指定 width
和 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
的自动宽度和高度,请考虑以下简单示例:
# 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
可视化"raw GPS points"会更简单。