【问题标题】:raster and ggplot map not quite lining up in R光栅和 ggplot 地图在 R 中没有完全对齐
【发布时间】:2016-03-18 19:15:54
【问题描述】:

我正在尝试使用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 制作地图,并覆盖栅格。

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

输出如下:

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

【问题讨论】:

  • 仅供参考,如果我这样做library(maps); map('usa'); plot(layer, add=TRUE),我会在基本图形中看到相同的偏移量。
  • 看起来边界与网格的左下角对齐。如果要使边框轮廓的 x 和 y 位置与中点对齐,请将 x 位置移动 x 网格间隔的一半和 y 网格间隔的一半。
  • 跟进@42 所说的,来自 ORNL 网站的元数据显示空间范围从 -124.0 到 -66.5 度经和 25.0 到 49.0 纬度,而我猜测 ggplot2 的默认值rasters 是相对于其中点绘制图块。

标签: r ggplot2 gis raster


【解决方案1】:

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

这个问题的解决方案是由 cmets 中的@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

都排好了!

【讨论】:

    【解决方案2】:

    根据 42 和 colin 的说法,ORNL 提供的文件不正确。你可能应该告诉他们这件事。该文件有:

    xllcorner -124
    yllcorner 25
    

    它显然应该在哪里:

    xllcorner -124.5
    yllcorner 25
    

    如果是这样,这意味着文件当前将 x 坐标指定为单元格边缘,将 y 坐标指定为单元格中心。从任何标准来看,这都不好。有一些格式可以让 x 和 y 都代表单元格边缘而不是单元格中心,但不能是两者之一;无论哪种方式,在使用的文件格式(arc-ascii)中,这是不允许的。

    纠正此错误的一个好方法是在创建RasterLayer 之后使用shift

    layer < raster("NADP_wet_deposition_nh4_0.5x0.5_grid_annual_R1.txt")
    layer <- shift(layer, -0.5, 0)
    

    然后继续。这是一个更通用的解决方案,因为它允许将栅格数据正确地用于 ggplotting 之外的其他目的。

    【讨论】:

    • 感谢您的回复!就这样我完全理解-当我将栅格转换为点时,栅格的投影和原始栅格都被移动了吗?我问是因为我使用 x/y GPS 坐标从特定站点的栅格中提取数据。如果不先进行此更正,此数据提取是否会关闭?
    • 原始栅格被移动。改正后,分数应该没问题;并且提取应该没问题(如果没有转变,确实是错误的)。
    • 这似乎不是开箱即用的。我收到错误:Error in shift(ndep.ORNL, -0.5, 0) : x must be a list, data.frame or data.table
    • 那是因为您使用该方法加载了另一个包,该方法将其隐藏在 raster 中。使用raster::shift( ) 来避免这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2015-03-19
    • 2020-11-13
    • 2020-02-28
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多