I can't manage to plot a raster and boundary of country (sp data frame) in the same plot. The data (lon,lat,value) are in mat data frame. The grid is not regular and comes from a climate change ncdf file.
Here is the ncdump original file :
7 variables (excluding dimension variables):
double lat[rlon,rlat]
bounds: lat_vertices
long_name: latitude coordinate
standard_name: latitude
units: degrees_north
double lat_vertices[vertices,rlon,rlat]
units: degrees_north
double lon[rlon,rlat]
bounds: lon_vertices
long_name: longitude coordinate
standard_name: longitude
units: degrees_east
double lon_vertices[vertices,rlon,rlat]
units: degrees_east
double time_bnds[bnds,time]
float hfss[rlon,rlat,time]
standard_name: surface_upward_sensible_heat_flux
long_name: Surface Upward Sensible Heat Flux
units: W m-2
original_name: HFX
cell_methods: time: mean
missing_value: 1.00000002004088e+20
_FillValue: 1.00000002004088e+20
grid_mapping: rotated_latitude_longitude
coordinates: lat lon
positive: up
int rotated_pole[]
grid_mapping_name: rotated_latitude_longitude
grid_north_pole_latitude: 39.25
grid_north_pole_longitude: -162
I try to plot this grid with raster and levelplot package.
library(raster)
library(rasterVis)
library(rworldmap)
data(wrld_simpl)
colnames(mat) <-c("lon","lat","value")
x.min=min(mat$lon)
x.max=max(mat$lon)
y.min=min(mat$lat)
y.max=max(mat$lat)
v.min=min(mat$value)
v.max=max(mat$value)
breaks=seq(v.min,v.max,2)
colour=rev(rainbow(length(breaks), alpha=1))
ras <- raster(nrows=453, ncols=453,xmn=x.min,xmx=x.max, ymn=y.min, ymx=y.max)
rvm <- matrix(mat[,3],453,453,byrow=TRUE)
ras[]=rvm[nrow(rvm):1,]
levelplot
levelplot(ras,at=breaks,col.regions=colour, margin = FALSE) +
layer(sp.polygons(wrld_simpl, fill = NA, col = "grey50"))
Of course, SP polygon (in WGS84) does not fit to the original ncdf grid. The original data frame is available here :
[https://drive.google.com/file/d/0Byd6YJGGADrfN255ZGhhTjRSUkU/view?usp=sharing][2]