Overlaying world map on a image plot in R -


i trying plot global map using latitude, longitude , grid data in r. using image , image.plot functions. additionally need overlay global coastline land area. not sure how place map on image of gridded data. map appearing bit shifted left in console , part not visible either. see sample code below random grid data.

remove(list=ls())  library(fields)  library(maps)  grid_lon<-c(0.5:1:359.5)  grid_lat<-c(-89.5:89.5)  temp1<-matrix(data = rexp(200, rate = 10), nrow = 360, ncol = 180)#random matrix  zlim=c(0,0.25)  par(oma=c( 3,0,0,0))# c(bottom, left, top, right)#plot margins  image(grid_lon,grid_lat,temp1,axes=false,xlab='',ylab='')  map("world", fill=true, col="white", bg="white", ylim=c(-90, 90),add=true)  title(main ='main title') image.plot(zlim=zlim,legend.only=true,horizontal=true,legend.mar=0.4,legend.shrink=0.4,legend.width=0.4,nlevel=64,axis.args = list(cex.axis =1,at=zlim, labels=zlim,mgp=c(1, 0, 0),tck=0),smallplot=c(.25,.72, 0,.030),     legend.args=list( text=expression(textstyle(atop('anomaly',     paste('(meters)')),cex.main=1.2)),cex=1.2, side=1, line=1.6)     )#end image.plot  box() 

in general, when working maps preferable use spatial objects, projection method can defined. coherence map better guaranteed. since working filled grid, obvious choice use raster package raster. code become:

require (raster) require (maps) temp1<-matrix(data = rexp(180*360, rate = 10), nrow = 360, ncol = 180)  #random matrix r<-raster(temp1,xmn=-179.5,xmx=179.5,ymn=-89.5,ymx=89.5,crs="+proj=longlat +datum=wgs84") plot(r) map("world",add=t,fill=true, col="white", bg="white") 

edit

this code not take account data comes 360*180 matrix, while desirable plot (map) 180*360 matrix. transposing risky because may result in upside-down image. in order sure right coordinates associated right values, can explicitly associate them, , afterwards transform spatial object. for-loop doing in code below slow, maybe can made more efficient, job.

require (raster) require (maps) # basic data, in code given grid_lon<-seq(0.5,359.5,1) grid_lat<-seq(-89.5,89.5,1) temp1<-matrix(data = rexp(200, rate = 10), nrow = 360, ncol = 180)#random matrix # transform data frame, coords associated values tt<-data.frame(lon=rep(na,64800),lat=rep(na,64800),z=rep(na,64800)) ct<-0 (i in 1:360){   (j in 1:180){     ct<-ct+1     tt$lon[ct]<-grid_lon[i]     tt$lat[ct]<-grid_lat[j]     tt$z[ct]<-temp1[i,j]   } } # transform spatial structure coordinates(tt)<- ~lon+lat # make spatial structure gridded gridded(tt)<-true # transform raster r<-raster(tt) projection(r)<-crs("+proj=longlat +datum=wgs84") # plot plot(r) map("world2",add=t,fill=true, col="white", bg="white") 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -