Search code examples
rggplot2aspect-ratiocartography

Get aspect ratio for lat-long plots


I know R has various ways to plot maps properly with projections. But just for a quick 'good enough' result using base functions, is there a function to calculate the aspect ratio for a particular latitude that if provided to R's typical asp arguments will approximate the right plot? i.e. Something equivalent to ggplot2's coord_quickmap method. Grateful for any suggestions.


Solution

  • If what coord_quickmap provides is close enough for you, then you can do:

    library(ggplot2)
    library(maps)
    library(mapdata)
    
    # shamelessly stolen from coord_quickmap
    
    map_aspect = function(x, y) {
      x.center <- sum(range(x)) / 2
      y.center <- sum(range(y)) / 2
      x.dist <- ggplot2:::dist_central_angle(x.center + c(-0.5, 0.5), rep(y.center, 2))
      y.dist <- ggplot2:::dist_central_angle(rep(x.center, 2), y.center + c(-0.5, 0.5))
      y.dist / x.dist
    }
    

    What ggplot would do:

    ggplot(data.frame(state.center)) + geom_point(aes(x=x, y=y)) + coord_quickmap()
    

    enter image description here

    Same thing, now, in base:

    plot(state.center$x, state.center$y,
         asp=map_aspect(state.center$x, state.center$y))
    

    enter image description here

    Remember, though, that coord_quickmap (and, hence, the calculated ratio from it) was designed for small areas. I post some resources for being able to pick map projections here: here which will work great with spTransform & base).