光栅和 ggplot map 在 R 中没有完全对齐

标签 r ggplot2 gis raster

我正在尝试使用 ggplot2 绘制空间栅格.

require(raster)
require(ggplot2)

下载数据,使用 raster 作为栅格加载包裹。有关此数据产品的更多详细信息,请访问 here .然后将栅格转换为点,使其与 ggplot 配合良好。 .
system('wget https://www.dropbox.com/s/7jsdqgc9wjcdznv/NADP_wet_deposition_nh4_0.5x0.5_grid_annual_R1.txt') 
layer<- raster("path/to/raster/NADP_wet_deposition_nh4_0.5x0.5_grid_annual_R1.txt") #you need to specify your own path here, wherever the downloaded file is saved. 
raster.points <- rasterToPoints(layer)
raster.points <- data.frame(raster.points)
colnames(raster.points) <-c('x','y','layer')

现在使用 ggplot2制作 map ,然后铺在光栅上。
mp <- NULL
#grab US map and choose colors
map.US <- borders("usa", colour='white',fill='black', lwd=0.4)
mp <- ggplot(data=raster.points, aes(y=y, x=x)) 
mp <- mp + map.US
mp <- mp + geom_raster(aes(fill=layer)) 
mp <- mp + theme(axis.text.y=element_blank(),
                axis.text.x=element_blank(),
                axis.title.y=element_blank(),
                axis.title.x=element_blank(),
                axis.ticks=element_blank(), 
                panel.background = element_rect(fill='black'),
                plot.background = element_rect(fill='black'),
                panel.grid.major=element_blank(),
                panel.grid.minor=element_blank())
mp

输出如下所示:

enter image description here

如您所见,事情几乎一致,但不完全一致。一切都略微向右移动。这可能是什么原因造成的,我该如何解决?

最佳答案

根据 ORNL 文档,Ndep 图的边界实际上与网格的左下角对齐。要使 x 和 y 位置与中点对齐(默认为 ggplot ),您需要将 x 位置移动 1 个网格间隔。因为在这种情况下网格间隔是 0.5 度,所以我从我的 x 坐标向量中减去了半度。

@42 在评论中建议了此问题的解决方案。

所以,和以前一样,下载数据:

system('wget https://www.dropbox.com/s/7jsdqgc9wjcdznv/NADP_wet_deposition_nh4_0.5x0.5_grid_annual_R1.txt') 
layer<- raster("path/to/raster/NADP_wet_deposition_nh4_0.5x0.5_grid_annual_R1.txt") #you need to specify your own path here, wherever the downloaded file is saved. 
raster.points <- rasterToPoints(layer)
raster.points <- data.frame(raster.points)
colnames(raster.points) <-c('x','y','layer')

至关重要的是,从坐标的 x 向量中减去半度。
raster.points$x <- raster.points$x - 0.5

现在,继续绘制:
mp <- NULL
#grab US map and choose colors
map.US <- borders("usa", colour='white',fill='black', lwd=0.4)
mp <- ggplot(data=raster.points, aes(y=y, x=x)) 
mp <- mp + map.US
mp <- mp + geom_raster(aes(fill=layer)) 
mp <- mp + theme(axis.text.y=element_blank(),
                axis.text.x=element_blank(),
                axis.title.y=element_blank(),
                axis.title.x=element_blank(),
                axis.ticks=element_blank(), 
                panel.background = element_rect(fill='black'),
                plot.background = element_rect(fill='black'),
                panel.grid.major=element_blank(),
                panel.grid.minor=element_blank())
mp

都排好队!
enter image description here

关于光栅和 ggplot map 在 R 中没有完全对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36092589/

相关文章:

algorithm - 如何找到网格中的所有节点

java - 保留:连接被拒绝:连接

r - 无法安装最新版本的 R 包

r - 有没有办法将自定义 R 功能操作添加到 DT 中的按钮?

r - 如何指定ecdf ggplot2中线和点的颜色

python - 如何使用 GDAL 将操纵的栅格值写入 ASCII 网格?

javascript - 用新数据更新 d3 图

r - 如何让dput删除多余的数据?

r - 使用plotly绘制多个ggplot2图

r - 使用 ggplot 绘制直方图中条形观察值的数量