R- 用国家轮廓绘制 2.5 网格 netcdf 数据

R- plotting 2.5 grid netcdf data with country contour

我正在尝试绘制具有 2.5 x 2.5 网格且顶部为国家等高线的降水数据,数据可在以下 link 中找到:https://www.esrl.noaa.gov/psd/data/gridded/data.cmap.html "Mean (Enhanced Monthly)"

我使用的答案来自:R - Plotting netcdf climate data。但是我得到一个错误。

这是我所做的:

library(ncdf4)
ncpath <- "C:/Users/"
ncname <- "precip.mon.mean"
ncfname <- paste(ncpath,ncname,".nc",sep="")
ncin <- nc_open(ncfname)

lon <- ncvar_get(ncin, "lon")
nlon <- dim(lon)

lat <- ncvar_get(ncin, "lat")
nlat <- dim(lat)

dname <-"precip"
ppt_array <- ncvar_get(ncin,dname)
dim(ppt_array)

pres <- ppt_array[ , ,25:444] 
precip <- array(pres, , dim=c(nlon, nlat, 12, ano)) 
prec <- precip[97:115,21:34, ,1:ano] #I just want a piece of the map

这是我遇到的问题:

latlat <- rev(lat)
precipit <- prec[ , ,1,1] %Just to see if it works
lonlon <- lon-180
image(lonlon,latlat,precipit) 
library(maptools)
data(wrld_simpl) 

#however I don't know if this will work to plot just a portion of the map  
plot(wrld_simpl,add=TRUE)

我遇到了几个错误,有人可以帮忙吗?

编辑: 我得到的错误是:

> image(lonlon,latlat,precipit)
Error in image.default(lonlon, latlat, precipit) : 
  increasing 'x' and 'y' values expected
> library(maptools)
> data(wrld_simpl)
> plot(wrld_simpl,add=TRUE)
Error in polypath(x = mcrds[, 1], y = mcrds[, 2], border = border, col = col,  : 
  plot.new has not been called yet

有几件事需要解决:

1) ano 似乎没有在任何地方定义。也许它是交互式定义的?

precip <- array(pres, , dim=c(nlon, nlat, 12, ano)) 

2) 您似乎打算添加评论但使用了 infix operator - 将其替换为 #,如下所示:

precipit <- prec[ , ,1,1] # Just to see if it works

3) 如果你只想拥有地图的一部分,你可以确保 latlon 数组都匹配你想要显示的区域(实质上是裁剪世界地图) 或在要突出显示的区域之外定义 NAs (这将看起来类似于地图 here)