Fixing maps library data for Pacific centred (0°-360° longitude) display

You could use the fact that internally, a map object returned by the map() function can be recalculated and used again in the map() function. I’d create a list with individual polygons, check which ones have very different longitude values, and rearrange those ones. I gave an example of this approach in the function below*, which allows something like :

plot.map("world", center=180, col="white",bg="gray",
   fill=TRUE,ylim=c(-60,90),mar=c(0,0,0,0))

to get

Corrected map center 180

If I were you, I’d shift everything a bit more, like in :

plot.map("world", center=200, col="white",bg="gray",
   fill=TRUE,ylim=c(-60,90),mar=c(0,0,0,0))

Corrected map center 200

The function :

plot.map<- function(database,center,...){
    Obj <- map(database,...,plot=F)
    coord <- cbind(Obj[[1]],Obj[[2]])

    # split up the coordinates
    id <- rle(!is.na(coord[,1]))
    id <- matrix(c(1,cumsum(id$lengths)),ncol=2,byrow=T)
    polygons <- apply(id,1,function(i){coord[i[1]:i[2],]})

    # split up polygons that differ too much
    polygons <- lapply(polygons,function(x){
        x[,1] <- x[,1] + center
        x[,1] <- ifelse(x[,1]>180,x[,1]-360,x[,1])
        if(sum(diff(x[,1])>300,na.rm=T) >0){
          id <- x[,1] < 0
          x <- rbind(x[id,],c(NA,NA),x[!id,])
       }
       x
    })
    # reconstruct the object
    polygons <- do.call(rbind,polygons)
    Obj[[1]] <- polygons[,1]
    Obj[[2]] <- polygons[,2]

    map(Obj,...)
}

*Note that this function only takes positive center values. It’s easily adapted to allow for center values in both directions, but I didn’t bother anymore as that’s trivial.

Leave a Comment