从R中的.netcdf数据创建绘图

时间:2015-04-13 06:08:00

标签: r plot

我在印度尼西亚的水中有一个含有4维度(lon,lat,深度和时间)的盐度的NetCDF文件。我想根据我的数据创建一张地图,但它看起来并不合适,与海岸线不完全匹配。 在此处下载数据:https://onedrive.live.com/redir?resid=6FFDD661570C7D0A%21177 输出地图:https://onedrive.live.com/redir?resid=6FFDD661570C7D0A%21176

# This is my script
library (ncdf)
library (raster)
library (sp)

setwd ('D:/work')
bio <- open.ncdf('data.nc')
print (bio)

sal.dat <- get.var.ncdf(bio,"salinity")
sal0 <- brick(sal.dat[,,1,])
extent(sal0) <- c(105,110,-5,0)
projection(sal0) <- CRS("+proj=longlat +datum=WGS84")

#plot image
image (sal0)

# load coastal lines of Indonesia:
con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/IDN_adm0.RData")
load(file = con)
close(con)

# plot coastal lines:
plot(gadm, add = T)

1 个答案:

答案 0 :(得分:0)

您可以将rasterVis包用于此目的:

# load needed librairies
library(rasterVis)
library(rgeos)

# open the data
salinity <- brick("data.nc", varname = "salinity")

# load coastal lines of Indonesia:
con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/IDN_adm0.RData")
load(file = con)
close(con)

# clip the coastline to the study area
CP <- as(extent(salinity), "SpatialPolygons")
proj4string(CP) <- CRS(proj4string(gadm))
gadm <- gIntersection(gadm, CP, byid=TRUE)

# map
levelplot(subset(salinity, 1), margin = FALSE) +
layer(sp.polygons(gadm, fill='transparent', col='black'))

enter image description here