Search code examples
rprojection

How to show values of long and lat on a map?


I have a file with a projection of EPSG:3410 that I want simply to plot using R. As the projection is in meters, I have a problem with the lat and long values (axes).You can see on the map that instead of 90 -90 180 -180 there are other numbers.

the code to plot this map:

       con1 <- file("C:\\Users\\data.bin","rb")
       r = raster(y)
      extent(r) = extent(c(xmn=-17334194,xmx=17334194,ymn=-7356860,ymx=7310585))
      plot(r)
      plot(wbuf, add=TRUE)
  • How I can replace those unusual values on the map with 90 -90 180 -180 (so no changes on the original file)
  • How I can get rid of the space up and bottom of the map?

Solution

  • Here's some workarounds to avoid the white space and to add the axis labels. Since both maps are in metres, it's not easy to plot lat-lon labels.

    library(raster) # for xmin, ymin etc.
    
    # plot raster without axes and box
    plot(r, asp=1, axes=F, box=F)
    
    # add polygons
    plot(wbuf, add=TRUE, axes=F)
    
    # draw bounding box at raster limits 
    rect(xmin(r), ymin(r), xmax(r), ymax(r))
    

    To add lat-lon ticks, we need to convert from the EPSG:3410 (metres) projection to lat-lon (degrees). We can create dummy spatial points in latlon and convert those to 3410, then extract the converted coordinates to add the ticks

    # y axis:
    x = -180
    y = seq(-90,90,30) # you can adapt this sequence if you want more or less labels
    S <- SpatialPoints(cbind(x,y), proj4string = CRS("+proj=longlat +datum=WGS84"))
    S2<- spTransform(S, CRS(" +proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +a=6371228 +b=6371228 +no_defs ")) 
    axis(side = 2, pos=xmin(r), at = S2@coords[,'y'], lab=y, las=1)
    
    
    # x axis
    y = -90
    x = seq(-180,180,30)
    S <- SpatialPoints(cbind(x,y), proj4string = CRS("+proj=longlat +datum=WGS84"))
    S2<- spTransform(S, CRS(" +proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +a=6371228 +b=6371228 +no_defs "))
    axis(side = 1, pos=ymin(r), at = S2@coords[,'x'], lab=x)
    

    Another option is to reproject the raster, so the maps are lat-lon (degrees):

    # reproject and plot raster
    r2 = projectRaster(r, crs=CRS("+proj=longlat +datum=WGS84"))
    plot(r2, axes=F, box=F)
    
    # there's an option to disable internal boundaries, which is nice
    map(database = 'world', regions = '', interior = F, add=T)
    
    # still have to add bounding box and axes
    rect(xmin(r), ymin(r), xmax(r), ymax(r))
    axis(1, pos = ymin(r2))
    axis(2, pos= xmin(r2), las=1)