|
1 | 1 | #' @include geom-map.r
|
2 | 2 | NULL
|
3 | 3 |
|
4 |
| -#' Annotation: a maps |
| 4 | +#' Annotation: a map |
5 | 5 | #'
|
6 |
| -#' Display a fixed map on a plot. |
| 6 | +#' Display a fixed map on a plot. This function predates the [`geom_sf()`] |
| 7 | +#' framework and does not work with sf geometry columns as input. However, |
| 8 | +#' it can be used in conjunction with `geom_sf()` layers and/or |
| 9 | +#' [`coord_sf()`] (see examples). |
7 | 10 | #'
|
8 |
| -#' @param map data frame representing a map. Most map objects can be |
9 |
| -#' converted into the right format by using [fortify()] |
10 |
| -#' @param ... other arguments used to modify aesthetics |
| 11 | +#' @param map Data frame representing a map. See [`geom_map()`] for |
| 12 | +#' details. |
| 13 | +#' @param ... Other arguments used to modify visual parameters, such as |
| 14 | +#' `colour` or `fill`. |
11 | 15 | #' @export
|
12 | 16 | #' @examples
|
13 |
| -#' if (require("maps")) { |
14 |
| -#' usamap <- map_data("state") |
| 17 | +#' \dontrun{ |
| 18 | +#' if (requireNamespace("maps", quietly = TRUE)) { |
| 19 | +#' # location of cities in North Carolina |
| 20 | +#' df <- data.frame( |
| 21 | +#' name = c("Charlotte", "Raleigh", "Greensboro"), |
| 22 | +#' lat = c(35.227, 35.772, 36.073), |
| 23 | +#' long = c(-80.843, -78.639, -79.792) |
| 24 | +#' ) |
15 | 25 | #'
|
16 |
| -#' seal.sub <- subset(seals, long > -130 & lat < 45 & lat > 40) |
17 |
| -#' ggplot(seal.sub, aes(x = long, y = lat)) + |
18 |
| -#' annotation_map(usamap, fill = NA, colour = "grey50") + |
19 |
| -#' geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat)) |
20 |
| -#' } |
| 26 | +#' p <- ggplot(df, aes(x = long, y = lat)) + |
| 27 | +#' annotation_map( |
| 28 | +#' map_data("state"), |
| 29 | +#' fill = "antiquewhite", colour = "darkgrey" |
| 30 | +#' ) + |
| 31 | +#' geom_point(color = "blue") + |
| 32 | +#' geom_text( |
| 33 | +#' aes(label = name), |
| 34 | +#' hjust = 1.105, vjust = 1.05, color = "blue" |
| 35 | +#' ) |
21 | 36 | #'
|
22 |
| -#' if (require("maps")) { |
23 |
| -#' seal2 <- transform(seal.sub, |
24 |
| -#' latr = cut(lat, 2), |
25 |
| -#' longr = cut(long, 2)) |
| 37 | +#' # use without coord_sf() is possible but not recommended |
| 38 | +#' p + xlim(-84, -76) + ylim(34, 37.2) |
26 | 39 | #'
|
27 |
| -#' ggplot(seal2, aes(x = long, y = lat)) + |
28 |
| -#' annotation_map(usamap, fill = NA, colour = "grey50") + |
29 |
| -#' geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat)) + |
30 |
| -#' facet_grid(latr ~ longr, scales = "free", space = "free") |
31 |
| -#' } |
| 40 | +#' if (requireNamespace("sf", quietly = TRUE)) { |
| 41 | +#' # use with coord_sf() for appropriate projection |
| 42 | +#' p + |
| 43 | +#' coord_sf( |
| 44 | +#' crs = st_crs(3347), |
| 45 | +#' xlim = c(-84, -76), |
| 46 | +#' ylim = c(34, 37.2) |
| 47 | +#' ) |
| 48 | +#' |
| 49 | +#' # you can mix annotation_map() and geom_sf() |
| 50 | +#' nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) |
| 51 | +#' p + |
| 52 | +#' geom_sf( |
| 53 | +#' data = nc, inherit.aes = FALSE, |
| 54 | +#' fill = NA, color = "black", size = 0.1 |
| 55 | +#' ) + |
| 56 | +#' coord_sf(crs = st_crs(3347)) |
| 57 | +#' }}} |
32 | 58 | annotation_map <- function(map, ...) {
|
33 | 59 | # Get map input into correct form
|
34 | 60 | if (!is.data.frame(map)) {
|
|
0 commit comments