Get aspect ratio for lat-long plots
Asked Answered
S

1

5

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.

Smoothen answered 31/7, 2015 at 12:11 Comment(3)
Can you give an example of how you're doing map plots in base (i.e. where you're getting the data from)?Trigon
Also, by default, maps::map will use a rectangular projection with the aspect ratio chosen so that longitude and latitude scales are equivalent at the center of the picture. Lemme post an example of how that's pretty much equivalent to coord_quickmap.Trigon
Plot example plot(state.center$x, state.center$y, asp=1.6)Smoothen
T
8

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).

Trigon answered 31/7, 2015 at 12:45 Comment(1)
Spot on! Slightly confused why the longitude inputs are necessary but hey. ggplot(data.frame(state.center)) + geom_point(aes(x=x, y=y)) + coord_fixed(ratio = ar) will demonstrate it's the exact same plotSmoothen

© 2022 - 2024 — McMap. All rights reserved.