r - R - 如何绘制 Openstreetmap 基础层并将其与 ggplot2 中的栅格对象叠加?
问题描述
我需要你的帮助。除了一些(此处未列出)之外,我想在withsf objects
顶部绘制一个栅格图层。当我使用 a 时这很好用,但当我使用如下转换的栅格时就不行了。结果显示了一个扭曲的山体阴影栅格,包括奇怪的坐标。我有一种感觉,问题出在光栅文件中(如这里),但我现在不知道如何正确处理它。请考虑将山体阴影栅格作为任何栅格的示例。openstreetmap
ggplot2
sf object
CRS
这是一个可重现的示例:
library(ggplot2)
library(ggspatial)
library(rosm)
library(raster)
library(sf)
# create hillshade
alt = getData('alt', country='CHE', mask = FALSE)
slope = terrain(alt, opt='slope')
aspect = terrain(alt, opt='aspect')
hill = hillShade(slope, aspect, 45, 315)
plot(hill, col=grey(0:100/100), legend=FALSE, main='Switzerland')
#check CRS
sf::st_crs(hill)
raster::crs(hill)
#convert to ggplot useable format
hill_spdf <- as(hill, "SpatialPixelsDataFrame")
hill_df <- as.data.frame(hill_spdf)
colnames(hill_df) <- c("value", "x", "y")
#plot works
ggplot() +
ggspatial::annotation_map_tile("stamenwatercolor", zoom = 7, cachedir = system.file("rosm.cache", package = "ggspatial")) +
geom_sf(data = CH1, mapping = aes(fill = NAME_1), fill=NA, colour="black", size = 0.5, show.legend = FALSE,)
#plot doesn't work
ggplot() +
ggspatial::annotation_map_tile("stamenwatercolor", zoom = 7, cachedir = system.file("rosm.cache", package = "ggspatial")) +
geom_raster(data = CH0_df, aes(x=x, y=y, fill = layer), alpha = 0.5)
解决方案
推荐阅读
- python - 如何在 Windows 的 Odoo 12.0 中安装 Mass Editing
- python - 用分隔符作为空格分割字符串,但它在 doubleqoutes 中保留空格,在 Python 中也保留 doubleqoutes
- java - Spring Data JPA 如何为多个实体构建通用存储库?
- java - Date.getYear() 与 Calendar.get(Calendar.YEAR) - 实施变更。它会起作用吗?
- c# - 仅在发布版本中捕获控制台关闭事件时,.NET 应用程序崩溃
- c# - 带有 MySQL 的 C# - 尝试打开连接时出现错误:“Guid 应包含 32 位数字和 4 个破折号”
- git - 单个 dockerfiles GitHub 存储库 + Docker Hub 自动构建
- c# - 您如何使用 LINQ 将多个列表合并到一个列表中,但只使用所有列表中的共同点?
- sqlite - 从查询结果中的 json 对象中删除值为“NULL”的 K、V 对
- c# - 文件被什么都阻止了?