首页 > 解决方案 > R - 如何绘制 Openstreetmap 基础层并将其与 ggplot2 中的栅格对象叠加?

问题描述

我需要你的帮助。除了一些(此处未列出)之外,我想在withsf objects顶部绘制一个栅格图层。当我使用 a 时这很好用,但当我使用如下转换的栅格时就不行了。结果显示了一个扭曲的山体阴影栅格,包括奇怪的坐标。我有一种感觉,问题出在光栅文件中(如这里),但我现在不知道如何正确处理它。请考虑将山体阴影栅格作为任何栅格的示例。openstreetmapggplot2sf objectCRS

这是一个可重现的示例:

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)

标签: rggplot2sfr-rasterggspatial

解决方案


推荐阅读