我正在尝试绘制具有2.5 x 2.5网格且国家轮廓位于顶部的降水数据,此链接中提供了数据:https://www.esrl.noaa.gov/psd/data/gridded/data.cmap.html“平均值(每月增强量)”
我使用的答案是: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
答案 0 :(得分:0)
有几件事需要修复:
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)如果您只想拥有地图的一部分,可以确保lat
和lon
数组都与您要显示的区域匹配(基本上裁剪世界地图)或者在要突出显示的区域之外定义NA(这将类似于地图here)