簡體   English   中英

在R中具有不規則邊界的地圖上繪制插值數據

[英]Plotting interpolated data on map with irregular boundaries in R

我無法在邊界不規則的地圖上繪制我的結果。 我使用 IDW 方法對氣候數據進行了插值,如下所示:

library(ggplot2)
library(gstat)
library(sp)
library(maptools)


df<-my_data

coordinates(df) = ~X + Y
x.range <- as.numeric(c(20.375, 31.375))
y.range <- as.numeric(c(52.375, 61.375))

grd2 <- expand.grid(X = seq(from = x.range[1], to = x.range[2], by = 0.01), Y = seq(from = y.range[1], to = y.range[2], by = 0.01))
coordinates(grd2) <- ~X + Y
gridded(grd2) <- TRUE
plot(grd2, cex = 1.5, col = "grey")
points(df, pch = 1, col = "red", cex = 1)

df2<-my_data
colnames(df2)<- c("ID", "lon", "lat", "vari")
idw2 <- idw(formula = vari ~ 1, locations = df, newdata = grd2)
idw.output2 = as.data.frame(idw2)
names(idw.output2)[1:3] <- c("lon", "lat", "var1.pred")

現在我想把面具放在這些 idw 結果上。 過去,我一直只使用一個國家的邊界​​,沒有任何問題。 代碼:

mymap <- readOGR("countryy.shp", layer="countryy")
summary(mymap)
wgsmap <- spTransform(mymap, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
wgsmap.contour <- fortify(wgsmap)

library(raster)

idw.r <- rasterFromXYZ(idw.output2[, c("lon", "lat", "var1.pred")])
idw.crp <- crop(idw.r, wgsmap)
idw.msk <- mask(idw.crp, wgsmap)
idw.msk.dfr <- as.data.frame(rasterToPoints(idw.msk))
names(idw.msk.dfr)[1:2] <- c("lon", "lat")

#result
ggplot() + geom_tile(data = idw.msk.dfr, alpha = 0.8, aes(x = lon, y = lat, fill = round(var1.pred, 0))) + scale_fill_gradient(low = "cyan", high = "orange") + geom_path(data = wgsmap.contour, aes(long, lat, group = group), colour = "grey") + geom_point(data = df2, aes(x = lon, y = lat), shape = 18, colour = "red")

現在我需要戴上一個帶有新邊界的面具,包括幾個國家:

x.range <- as.numeric(c(20.375, 31.375)) y.range <- as.numeric(c(52.375, 61.375))

我不知道該怎么做。 我應該根據我的坐標矩形以某種方式修剪整個世界的 ESRI shapefile 嗎? 或者,也許我可以使用來自 ggspatial 的簡單地圖:

library("rnaturalearth")
library("rnaturalearthdata")
library(ggspatial)
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)

europe <- ggplot(data = world) +geom_sf() + coord_sf(xlim = c(20.375, 31.375), ylim = c(52.375, 61.375), expand = FALSE)

如果有人可以幫助我解決這個問題,我將不勝感激。

問題是使用包“GADMTools”出售的。

joinmap <- gadm_sp_loadCountries(c("RUS","LTU","LVA", "EST", "FIN", "POL", "BLR"),  basefile = "./")

studyarea <- gadm_crop(joinmap, xmin=20.375, ymin=52.375, xmax=31.375, ymax=61.375)

gadm_exportToShapefile(studyarea, "path for shapefile")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM