1

我正在尝试在地理地图上绘制热图以显示变量的地理分布。带有荒谬数据的最低工作代码如下:

library(ggmap)
library(osmdata)
box <- c(left = 2.075, bottom = 41.325, right = 2.25, top = 41.47)
map <- get_stamenmap(bbox = box, maptype = "terrain-lines", zoom = 13)

lon_grid <- seq(2.075, 2.25, length.out = 30)
lat_grid <- seq(41.325, 41.47, length.out = 30)
grid <- expand.grid(lon_grid, lat_grid)
z <- c(rep(NA, 30^2/2), rnorm(30^2/2))
dataset <- cbind(grid, z)

ggmap(map) ### Plot 1

ggmap(map) + ### Plot 2
  geom_raster(data = dataset, aes(x = Var1, y = Var2, fill = z), alpha = 0.5,  interpolate = TRUE) +
  scale_fill_viridis_c(option = "magma", na.value = "transparent") +
  coord_equal()

第一张地图看起来很完美:整齐、干净、线条清晰。 第一张地图

The second one, having added the geom_raster layer, looks (besides wider) slightly blurred, not that crisp. See that the geom_raster line adds a whitish layer ontop of the map (if you look closely it does not even cover it totally). It is absolutely awful and I would like to remove it, or, in other words, I would like it to take a "transparent" colour when the tile produced by geom_raster takes a NA value. 第二张地图

Any ideas?

4

1 回答 1

3

如果我正确理解了这个问题,那么NA光栅中的似乎并不完全透明。看看 in scale_fill_viridis_cchange tona.value = NA是否符合您的要求。

library(ggmap)
library(osmdata)
box <- c(left = 2.075, bottom = 41.325, right = 2.25, top = 41.47)
map <- get_stamenmap(bbox = box, maptype = "terrain-lines", zoom = 13)

lon_grid <- seq(2.075, 2.25, length.out = 30)
lat_grid <- seq(41.325, 41.47, length.out = 30)
grid <- expand.grid(lon_grid, lat_grid)
z <- c(rep(NA, 30^2/2), rnorm(30^2/2))
 <- cbind(grid, z)

ggmap(map) + ### Plot 2
  geom_raster(data = dataset, aes(x = Var1, y = Var2, fill = z), alpha = 0.5,  interpolate = TRUE) +
  scale_fill_viridis_c(option = "magma", na.value = NA) +
  coord_equal()

这是它的输出: 在此处输入图像描述

于 2021-05-06T20:30:36.743 回答