File: coord-quickmap.R

package info (click to toggle)
r-cran-ggplot2 3.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,748 kB
  • sloc: sh: 15; makefile: 5
file content (37 lines) | stat: -rw-r--r-- 1,206 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#' @inheritParams coord_cartesian
#' @export
#' @rdname coord_map
coord_quickmap <- function(xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") {
  ggproto(NULL, CoordQuickmap,
    limits = list(x = xlim, y = ylim),
    expand = expand,
    clip = clip
  )
}

#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
CoordQuickmap <- ggproto("CoordQuickmap", CoordCartesian,

  aspect = function(ranges) {
    # compute coordinates of center point of map
    x.center <- sum(ranges$x.range) / 2
    y.center <- sum(ranges$y.range) / 2

    # compute distance corresponding to 1 degree in either direction
    # from the center
    x.dist <- dist_central_angle(x.center + c(-0.5, 0.5), rep(y.center, 2))
    y.dist <- dist_central_angle(rep(x.center, 2), y.center + c(-0.5, 0.5))
    # NB: this makes the projection correct in the center of the plot and
    #     increasingly less correct towards the edges. For regions of reasonnable
    #     size, this seems to give better results than computing this ratio from
    #     the total lat and lon span.

    # scale the plot with this aspect ratio
    ratio <- y.dist / x.dist

    diff(ranges$y.range) / diff(ranges$x.range) * ratio
  }
)