除了 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)) +

我们还可以删除图例键中的背景灰色方块以获得更好的视觉效果(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
and height = min(abs(diff(d$lat))) ~ 3.292719e-05
(由于runif
,实际值可能会有所不同)。
鉴于示例或 GPS 坐标表示“原始点”而不是“每个区域的中心”,这种自动化将导致问题,因为两个相邻“原始点”之间的差距可能太窄而无法绘制像素。在这种情况下,我们将不得不手动指定width
andheight
并记住瓷砖会重叠。
考虑您的示例,其中:
+ 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 点”会更简单。
