|
3 | 3 |
|
4 | 4 | #' Polygons from a reference map
|
5 | 5 | #'
|
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). |
7 | 11 | #'
|
8 | 12 | #' @eval rd_aesthetics("geom", "map")
|
9 | 13 | #' @export
|
|
14 | 18 | #' @inheritParams layer
|
15 | 19 | #' @inheritParams geom_point
|
16 | 20 | #' @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. |
21 | 27 | #'
|
22 | 28 | #' ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
|
23 | 29 | #'
|
|
44 | 50 | #' geom_map(aes(map_id = id), map = positions) +
|
45 | 51 | #' expand_limits(positions) + ylim(0, 3)
|
46 | 52 | #'
|
47 |
| -#' # Better example |
| 53 | +#' # Now some examples with real maps |
48 | 54 | #' if (require(maps)) {
|
49 | 55 | #'
|
50 | 56 | #' crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
|
|
56 | 62 | #' crimes_long <- do.call("rbind", vars)
|
57 | 63 | #'
|
58 | 64 | #' states_map <- map_data("state")
|
| 65 | +#' |
| 66 | +#' # without geospatial coordinate system, the resulting plot |
| 67 | +#' # looks weird |
59 | 68 | #' ggplot(crimes, aes(map_id = state)) +
|
60 | 69 | #' geom_map(aes(fill = Murder), map = states_map) +
|
61 | 70 | #' expand_limits(x = states_map$long, y = states_map$lat)
|
62 | 71 | #'
|
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) |
68 | 91 | #' }
|
69 | 92 | geom_map <- function(mapping = NULL, data = NULL,
|
70 | 93 | stat = "identity",
|
|
0 commit comments