我正在使用tidyterra
在ggplot2
中绘制一个带有点的光栅Map:
library(ggplot2)
library(rnaturalearth)
library(terra)
library(tidyterra)
backmap <- ne_download(scale=50, type="MSR_50M", category="raster") # will download a 17MB map
data <- data.frame(longitude=runif(10, -180, 180), latitude=runif(10, -90, 90)) # simulate points
ggplot(data, aes(longitude, latitude)) +
geom_spatraster(data=backmap) +
geom_point() +
coord_sf(crs='+proj=lonlat', expand=F)
字符串
的数据
我想在光栅Map上方绘制经纬网,因为现在它们在光栅Map下方,只有在Map不是完全不透明的情况下才可见:
ggplot(data, aes(longitude, latitude)) +
geom_spatraster(data=backmap, alpha=.5) +
geom_point() +
coord_sf(crs='+proj=lonlat', expand=F)
型
的
我知道我可以像this SO answer中描述的那样使用theme(panel.background=element_blank(), panel.ontop=T)
将网格置于所有内容之上,但是这样的话,我不喜欢网格之下的点。
我知道在this SO answer中建议的sf::st_graticule()
的解决方案,但想知道是否可以通过ggplot2
魔术师解决两次绘制相同网格的冗余。
2条答案
按热度按时间aiazj4mn1#
sf
包是你的好朋友。请注意示例图像中巴布亚新几内亚上方的点,它位于网格的前面。此外,最好将所有空间矢量数据(如点)转换为具有定义crs的sf
对象。我省略了光栅导入步骤,但保持对象名称不变:字符串
的数据
wribegjk2#
我在安装(tidy)terra时遇到了麻烦,所以我不知道这是否适用于您的数据对象,但通常情况下,您可以在栅格和点之间添加手动经纬网。
字符串
x1c 0d1x的数据
创建于2023-12-13使用reprex v2.0.2