栅格和 ggplot 地图在 R 中不太对齐
raster and ggplot map not quite lining up in R
我正在尝试使用 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
输出如下所示:
如您所见,几乎 一致,但不完全一致。一切都稍微向右移动。可能是什么原因造成的,我该如何解决?
根据 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
全部排队!
根据 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 以外的其他目的。
我正在尝试使用 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
输出如下所示:
如您所见,几乎 一致,但不完全一致。一切都稍微向右移动。可能是什么原因造成的,我该如何解决?
根据 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
全部排队!
根据 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 以外的其他目的。