R - 如何在 ggplot2 中绘制 Openstreetmap baselayer 并将其与光栅对象叠加?

问题描述

我需要你的帮助。除了一些sf objects(此处未列出)之外,我想在带有openstreetmapggplot2 之上绘制一个栅格图层。这在我使用 sf object 时工作正常,但当我使用如下转换的栅格时则不然。结果显示扭曲的山体阴影栅格,包括奇怪的坐标。我有一种感觉,问题出在光栅文件CRS (如 here),但我现在不知道如何正确处理它。请将山体阴影栅格视为任何栅格的示例。

这是一个可重现的例子:

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",package = "ggspatial")) +
          geom_raster(data = CH0_df,aes(x=x,y=y,fill = layer),alpha = 0.5)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)