栅格和ggplot地图在R中排列不完整

col*_*lin 8 gis r raster ggplot2

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

require(raster)
require(ggplot2)
Run Code Online (Sandbox Code Playgroud)

下载数据,使用raster包加载为栅格.有关此数据产品的更多详细信息,请访问此处.然后将光栅转换为点,以便它可以很好地运行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')
Run Code Online (Sandbox Code Playgroud)

现在ggplot2用来制作地图,然后躺在栅格上.

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
Run Code Online (Sandbox Code Playgroud)

输出如下所示:

在此输入图像描述

正如你所看到的,事情几乎排成一列,但并不完全.一切都略微向右移动.可能导致此问题的原因以及如何解决?

col*_*lin 5

根据 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')
Run Code Online (Sandbox Code Playgroud)

至关重要的是,从坐标的 x 向量中减去半度。

raster.points$x <- raster.points$x - 0.5
Run Code Online (Sandbox Code Playgroud)

现在,继续绘制:

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
Run Code Online (Sandbox Code Playgroud)

都排好队! 在此处输入图片说明