【发布时间】: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 是相对于其中点绘制图块。