r - geom_raster() 在地图上产生一个白色的表面
问题描述
我正在尝试在地理地图上绘制热图以显示变量的地理分布。带有荒谬数据的最低工作代码如下:
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?
解决方案
如果我正确理解了这个问题,那么NA
光栅中的似乎并不完全透明。看看 in scale_fill_viridis_c
change 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()
推荐阅读
- javascript - 轮播多项目:项目未对齐
- ios - 通过内容拥抱优先级解决歧义
- ibm-doors - 将项目中的所有工件复制到一个新文件夹并在 IBM RDNG 中从它们创建一个模块
- php - 上传后个人资料图片未在浏览器中更新
- java - Java HTTP 连接似乎保持打开状态
- sql - SQL - 仅从表中获取并非所有值都是空值的列
- javascript - 浏览器上的图像处理
- angular - Angular:复选框的表单组绑定在迁移到版本 6 后未设置默认值
- php - 如果输出被过滤,则 bash 脚本的 php 实时输出
- r - 避免评估错误:R SF 包中的 TopologyException 错误