plot - Plotting netcdf in R with correct grid -
my goal plot nitrate (no3) data on world map, using correct longitude , latitude these data.
there 2 netcdf files:
1. with data
2. with grid information
summary info on data: no3 array of length x*y*sigma no3_df 'x*y obs. of 3 variables' x = integer [180] y = integer [193] sigma = array[53]
i want @ sigma ('depth') 20. therefore did following:
# load needed libraries handle netcdf files library(ncdf) library(akima) # open data , grid files file1 <- open.ncdf(file.choose()) grid <- open.ncdf(file.choose()) # read relevant variables/parameters data file1 x <- get.var.ncdf(file1,varid="x") y <- get.var.ncdf(file1,varid="y") sigma <- get.var.ncdf(file1,varid="sigma") no3 <- get.var.ncdf(file1,varid="no3") sigma_plot <- no3[,,sigma=20] # read relevant variables/parameters grid file plon <- get.var.ncdf(grid,varid="plon") plat <- get.var.ncdf(grid,varid="plat") # each cell of sigma_plot corresponds 1 cell of plon , plat. <- array(c(plon,plat,sigma_plot),dim=c(180,193,3)) # b array containing each row: (longitude, latitude, value). b <- apply(a, 3, cbind) # not regular grid, interpolate regular grid. akima library c <- interp(b[,1],b[,2],b[,3], xo=seq(-180,180,1),yo=seq(-90,90,by=1), # tweak here resolution duplicate='mean') # y values duplicates ######### # plotting ######### # 1 works, doesn't have correct longitude , latitude: filled.contour(x,y,sigma_plot, col=rich.colors(18)) # try plot lon , lat filled.contour(c, col=rich.colors(30))
since filled.contour plot doesn't have correct longitude , latitude, use ggplot. however, don't know how this...
# , plotting ggplot ggplot(aes(x=plon_datafrm,y=plat_datafrm),data=no3_df) + geom_raster() + coord_equal() + scale_fill_gradient()
this doesn't seem work. net ggplot might reason, appreciate help.
library(ncdf) data <- open.ncdf(file1) no3 <- get.var.ncdf(data,varid="no3") sigma_plot <- no3[,,20] grid <- open.ncdf(file2) plon <- get.var.ncdf(grid,varid="plon") plat <- get.var.ncdf(grid,varid="plat")
contrary understood, each cell of sigma_plot corresponds 1 cell of plon , plat.
a <- array(c(plon,plat,a),dim=c(180,193,3)) b <- apply(a, 3, cbind)
now b array containing each row: (longitude, latitude, value). not regular grid, need interpolate regular grid. easiest way using interp
package akima
:
library(akima) c <- interp(b[,1],b[,2],b[,3], xo=seq(-180,180,1),yo=seq(-90,90,by=1), #you can tweak here resolution duplicate='mean') #for reasons entries duplicates, don t know how want handle it. image(c) #for instance, or filled.contour if prefer library(maptools) data(wrld_simpl) plot(wrld_simpl, add=true, col="white") #to add simple world map on top
Comments
Post a Comment