如何在 R 中使用 ggplot2 在投影地图上绘制插值数据

How to plot interpolating data on a projected map using ggplot2 in R

我想使用 ggplot2 在投影地图上绘制一些插值数据,几周来我一直在研究这个问题。希望有人能帮助我,非常感谢。可以在 https://www.dropbox.com/s/8wfgf8207dbh79r/gpr_000b11a_e.zip?dl=0 and https://www.dropbox.com/s/9czvb35vsyf3t28/Mydata.rdata?dl=0 .

找到 shapefile 和数据

首先,shapefile 最初使用 "lon-lat" 投影,我需要将其转换为阿尔伯斯等积 (aea) 投影。

library(automap)
library(ggplot2)
library(rgdal)
load("Mydata.rdata",.GlobalEnv)
canada2<-readOGR("gpr_000b11a_e.shp", layer="gpr_000b11a_e")
g <- spTransform(canada2, CRS("+proj=aea +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))
Borders=ggplot() +geom_polygon(data=g,aes(x=long,y=lat,group=group),fill='white',color = "black")
Borders

正如我们所见,我们可以正确地绘制国家。然后我想使用Kriging方法对数据进行插值,代码取自

coordinates(Mydata)<-~longitude+latitude
proj4string(Mydata)<-CRS("+proj=longlat +datum=NAD83")
sp_mydata<-spTransform(Mydata,CRS(proj4string(g)))
Krig=autoKrige(APPT~1,sp_mydata)
interp_data = as.data.frame(Krig$krige_output)
colnames(interp_data) = c("latitude","longitude","APPT_pred","APPT_var","APPT_stdev")
interp_data=interp_data[,1:3]
ggplot(data=interp_data, aes(x=longitude, y=latitude)) + geom_tile(aes(fill=APPT_pred),color=NA)

然后我们就可以看到插值数据图了。

最后我想合并这两个数字然后我得到以下错误:Error: Don't know how to add o to a plot

ggplot(data=interp_data, aes(x=longitude, y=latitude)) + geom_tile(aes(fill=APPT_pred),color=NA)+Borders 

我的问题是:如何在地图上绘制插值数据,然后添加网格线(经度和纬度)。另外,我想知道如何裁剪插值数据地图以适应整个加拿大地图。感谢您的帮助。

进一步挖掘后,我猜你可能想要这个:

Krig = autoKrige(APPT~1,sp_mydata)$krige_output
Krig = Krig[!is.na(over(Krig,as(g,"SpatialPolygons"))),]  # take only the points falling in poolygons
Krig_df = as.data.frame(Krig)
names(Krig_df) = c("APPT_pred","APPT_var","APPT_stdev","longitude","latitude")
g_fort = fortify(g)
Borders = ggplot() +
  geom_raster(data=Krig_df, aes(x=longitude, y=latitude,fill=APPT_pred))+
  geom_polygon(data=g_fort,aes(x=long,y=lat,group=group),
               fill='transparent',color = "black")+
  theme_bw()
Borders

给出:

唯一的问题是您在生成的地图中仍然有 "missing" 个插值区域(例如,在西部)。 这是因为,从 autokrige 帮助:

new_data: A sp object containing the prediction locations. new_data can be a points set, a grid or a polygon. Must not contain NA’s. If this object is not provided a default is calculated. This is done by taking the convex hull of input_data and placing around 5000 gridcells in that convex hull

因此,如果您不提供可行的新数据作为参数,则插值区域受输入数据集点的凸包限制(=无外推)。 这可以使用 sp 包中的 spsample 来解决:

library(sp)
ptsreg <- spsample(g, 4000, type = "regular")   # Define the ouput grid - 4000 points in polygons extent
Krig = autoKrige(APPT~1,sp_mydata, new_data = ptsreg)$krige_output
Krig = Krig[!is.na(over(Krig,as(g,"SpatialPolygons"))),]  # take only the points falling in poolygons
Krig_df = as.data.frame(Krig)
names(Krig_df) = c("longitude","latitude", "APPT_pred","APPT_var","APPT_stdev")
g_fort = fortify(g)
Borders = ggplot() +
  geom_raster(data=Krig_df, aes(x=longitude, y=latitude,fill=APPT_pred))+
  geom_polygon(data=g_fort,aes(x=long,y=lat,group=group),
               fill='transparent',color = "black")+
  theme_bw()
Borders

给出:

请注意,可以通过增加对 spsample 的调用中的插值点数量来移除靠近多边形边界的小 "holes"(因为这是一个缓慢的操作,我只问4000,在这里)

一个更简单的快速替代方法是使用包 mapview

library(mapview)
m1 <- mapview(Krig)
m2 <- mapview(g)
m2+m1

(您可能希望使用不太详细的多边形边界 shapefile,因为这很慢)

HTH!