Search code examples
rdictionaryplotflip

My map (in R) is upside down. How do I flip it back up?


I have an .nc file that I'm trying to plot a map out of. I got my data here: https://www.esrl.noaa.gov/psd/data/gridded/data.UDel_AirT_Precip.html (precip.mon.ltm.v301.nc). The latitude starts from 89.75 to -89.75. Longitude starts from 0.25 to 359.75. This is what I did:

File1<-Sys.glob("precip.mon.ltm*.nc")

tmfile<-File1

pr_file<-nc_open(tmfile)

lat=ncvar_get(pr_file,'lat')

lon=ncvar_get(pr_file,'lon')

time=ncvar_get(pr_file,'time')

precip=ncvar_get(pr_file,'precip')

image.plot(lon,lat,precip[,,1],main="Precipitation")

Error in image.default(..., breaks = breaks, add = add, col = col) : increasing 'x' and 'y' values expected

lat1<-sort(lat)

image.plot(lon,lat1,precip[,,1],main="Precipitation",zlim=c(0,20))

Then the map comes out, but upside down. ie, Antartica is on top. How do I turn my map the right way round?


Solution

  • A quick hack is to flip the data itself.

    image.plot(lon,lat,precip[,ncol(precip):1,1],main="Precipitation")
    

    The lon and lat variables, i believe, also need to be defined differently as the error suggests, e.g. (see the related question):

    lon = seq(1, 360, length.out = nrow(precip)) 
    lat = seq(1, 360, length.out = ncol(precip))
    

    Update: I, somehow, switched lat and lon. Please, try this now. And you will need to change the axes labels into something that would make sense.

    Precip