Skip to content

Commit 819d014

Browse files
authored
fixes #3721 (#4865)
1 parent 9816470 commit 819d014

File tree

4 files changed

+89
-24
lines changed

4 files changed

+89
-24
lines changed

R/coord-map.r

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#' Map projections
22
#'
3+
#' @description
4+
#' `r lifecycle::badge("superseded")`
5+
#'
36
#' `coord_map()` projects a portion of the earth, which is approximately
47
#' spherical, onto a flat 2D plane using any projection defined by the
58
#' `mapproj` package. Map projections do not, in general, preserve straight
69
#' lines, so this requires considerable computation. `coord_quickmap()` is a
710
#' quick approximation that does preserve straight lines. It works best for
811
#' smaller areas closer to the equator.
912
#'
10-
#' In general, map projections must account for the fact that the actual length
13+
#' Both `coord_map()` and `coord_quickmap()`
14+
#' are superseded by [`coord_sf()`], and should no longer be used in new
15+
#' code. All regular (non-sf) geoms can be used with `coord_sf()` by
16+
#' setting the default coordinate system via the `default_crs` argument.
17+
#' See also the examples for [`annotation_map()`] and [`geom_map()`].
18+
#'
19+
#' @details
20+
#'
21+
#' Map projections must account for the fact that the actual length
1122
#' (in km) of one degree of longitude varies between the equator and the pole.
1223
#' Near the equator, the ratio between the lengths of one degree of latitude and
1324
#' one degree of longitude is approximately 1. Near the pole, it tends

R/geom-map.r

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ NULL
33

44
#' Polygons from a reference map
55
#'
6-
#' This is pure annotation, so does not affect position scales.
6+
#' Display polygons as a map. This is meant as annotation, so it does not
7+
#' affect position scales. Note that this function predates the [`geom_sf()`]
8+
#' framework and does not work with sf geometry columns as input. However,
9+
#' it can be used in conjunction with `geom_sf()` layers and/or
10+
#' [`coord_sf()`] (see examples).
711
#'
812
#' @eval rd_aesthetics("geom", "map")
913
#' @export
@@ -14,10 +18,12 @@ NULL
1418
#' @inheritParams layer
1519
#' @inheritParams geom_point
1620
#' @examples
17-
#' # When using geom_polygon, you will typically need two data frames:
18-
#' # one contains the coordinates of each polygon (positions), and the
19-
#' # other the values associated with each polygon (values). An id
20-
#' # variable links the two together
21+
#' # First, a made-up example containing a few polygons, to explain
22+
#' # how `geom_map()` works. It requires two data frames:
23+
#' # One contains the coordinates of each polygon (`positions`), and is
24+
#' # provided via the `map` argument. The other contains the
25+
#' # other the values associated with each polygon (`values`). An id
26+
#' # variable links the two together.
2127
#'
2228
#' ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
2329
#'
@@ -44,7 +50,7 @@ NULL
4450
#' geom_map(aes(map_id = id), map = positions) +
4551
#' expand_limits(positions) + ylim(0, 3)
4652
#'
47-
#' # Better example
53+
#' # Now some examples with real maps
4854
#' if (require(maps)) {
4955
#'
5056
#' crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
@@ -56,15 +62,32 @@ NULL
5662
#' crimes_long <- do.call("rbind", vars)
5763
#'
5864
#' states_map <- map_data("state")
65+
#'
66+
#' # without geospatial coordinate system, the resulting plot
67+
#' # looks weird
5968
#' ggplot(crimes, aes(map_id = state)) +
6069
#' geom_map(aes(fill = Murder), map = states_map) +
6170
#' expand_limits(x = states_map$long, y = states_map$lat)
6271
#'
63-
#' last_plot() + coord_map()
64-
#' ggplot(crimes_long, aes(map_id = state)) +
65-
#' geom_map(aes(fill = value), map = states_map) +
66-
#' expand_limits(x = states_map$long, y = states_map$lat) +
67-
#' facet_wrap( ~ variable)
72+
#' # in combination with `coord_sf()` we get an appropriate result
73+
#' ggplot(crimes, aes(map_id = state)) +
74+
#' geom_map(aes(fill = Murder), map = states_map) +
75+
#' # crs = 5070 is a Conus Albers projection for North America,
76+
#' # see: https://epsg.io/5070
77+
#' # default_crs = 4326 tells coord_sf() that the input map data
78+
#' # are in longitude-latitude format
79+
#' coord_sf(
80+
#' crs = 5070, default_crs = 4326,
81+
#' xlim = c(-125, -70), ylim = c(25, 52)
82+
#' )
83+
#'
84+
#' ggplot(crimes_long, aes(map_id = state)) +
85+
#' geom_map(aes(fill = value), map = states_map) +
86+
#' coord_sf(
87+
#' crs = 5070, default_crs = 4326,
88+
#' xlim = c(-125, -70), ylim = c(25, 52)
89+
#' ) +
90+
#' facet_wrap(~variable)
6891
#' }
6992
geom_map <- function(mapping = NULL, data = NULL,
7093
stat = "identity",

man/coord_map.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_map.Rd

Lines changed: 34 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)