|
1 | 1 | #' Visualise sf objects
|
2 | 2 | #'
|
3 | 3 | #' This set of geom, stat, and coord are used to visualise simple feature (sf)
|
4 |
| -#' objects. For simple plots, you will only need `geom_sf` as it |
5 |
| -#' uses `stat_sf` and adds `coord_sf` for you. `geom_sf` is |
| 4 | +#' objects. For simple plots, you will only need `geom_sf()` as it |
| 5 | +#' uses `stat_sf()` and adds `coord_sf()` for you. `geom_sf()` is |
6 | 6 | #' an unusual geom because it will draw different geometric objects depending
|
7 | 7 | #' on what simple features are present in the data: you can get points, lines,
|
8 | 8 | #' or polygons.
|
| 9 | +#' For text and labels, you can use `geom_sf_text()` and `geom_sf_label()`. |
9 | 10 | #'
|
10 | 11 | #' @section Geometry aesthetic:
|
11 |
| -#' `geom_sf` uses a unique aesthetic: `geometry`, giving an |
| 12 | +#' `geom_sf()` uses a unique aesthetic: `geometry`, giving an |
12 | 13 | #' column of class `sfc` containing simple features data. There
|
13 | 14 | #' are three ways to supply the `geometry` aesthetic:
|
14 | 15 | #'
|
15 |
| -#' - Do nothing: by default `geom_sf` assumes it is stored in |
| 16 | +#' - Do nothing: by default `geom_sf()` assumes it is stored in |
16 | 17 | #' the `geometry` column.
|
17 | 18 | #' - Explicitly pass an `sf` object to the `data` argument.
|
18 | 19 | #' This will use the primary geometry column, no matter what it's called.
|
|
23 | 24 | #'
|
24 | 25 | #' @section CRS:
|
25 | 26 | #' `coord_sf()` ensures that all layers use a common CRS. You can
|
26 |
| -#' either specify it using the `CRS` param, or `coord_sf` will |
| 27 | +#' either specify it using the `CRS` param, or `coord_sf()` will |
27 | 28 | #' take it from the first layer that defines a CRS.
|
28 | 29 | #'
|
29 | 30 | #' @param show.legend logical. Should this layer be included in the legends?
|
|
32 | 33 | #'
|
33 | 34 | #' You can also set this to one of "polygon", "line", and "point" to
|
34 | 35 | #' override the default legend.
|
| 36 | +#' @seealso [stat_sf_coordinates()] |
35 | 37 | #' @examples
|
36 | 38 | #' if (requireNamespace("sf", quietly = TRUE)) {
|
37 | 39 | #' nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
|
|
70 | 72 | #' "+proj=laea +y_0=0 +lon_0=155 +lat_0=-90 +ellps=WGS84 +no_defs"
|
71 | 73 | #' )
|
72 | 74 | #' ggplot() + geom_sf(data = world2)
|
| 75 | +#' |
| 76 | +#' # To add labels, use geom_sf_label(). |
| 77 | +#' ggplot(nc_3857[1:3, ]) + |
| 78 | +#' geom_sf(aes(fill = AREA)) + |
| 79 | +#' geom_sf_label(aes(label = NAME)) |
73 | 80 | #' }
|
74 | 81 | #' @name ggsf
|
75 | 82 | NULL
|
@@ -257,6 +264,114 @@ geom_sf <- function(mapping = aes(), data = NULL, stat = "sf",
|
257 | 264 | )
|
258 | 265 | }
|
259 | 266 |
|
| 267 | +#' @export |
| 268 | +#' @rdname ggsf |
| 269 | +#' @inheritParams geom_label |
| 270 | +#' @inheritParams stat_sf_coordinates |
| 271 | +geom_sf_label <- function(mapping = aes(), data = NULL, |
| 272 | + stat = "sf_coordinates", position = "identity", |
| 273 | + ..., |
| 274 | + parse = FALSE, |
| 275 | + nudge_x = 0, |
| 276 | + nudge_y = 0, |
| 277 | + label.padding = unit(0.25, "lines"), |
| 278 | + label.r = unit(0.15, "lines"), |
| 279 | + label.size = 0.25, |
| 280 | + na.rm = FALSE, |
| 281 | + show.legend = NA, |
| 282 | + inherit.aes = TRUE, |
| 283 | + fun.geometry = NULL) { |
| 284 | + |
| 285 | + # Automatically determin name of geometry column |
| 286 | + if (!is.null(data) && is_sf(data)) { |
| 287 | + geometry_col <- attr(data, "sf_column") |
| 288 | + } else { |
| 289 | + geometry_col <- "geometry" |
| 290 | + } |
| 291 | + if (is.null(mapping$geometry)) { |
| 292 | + mapping$geometry <- as.name(geometry_col) |
| 293 | + } |
| 294 | + |
| 295 | + if (!missing(nudge_x) || !missing(nudge_y)) { |
| 296 | + if (!missing(position)) { |
| 297 | + stop("Specify either `position` or `nudge_x`/`nudge_y`", call. = FALSE) |
| 298 | + } |
| 299 | + |
| 300 | + position <- position_nudge(nudge_x, nudge_y) |
| 301 | + } |
| 302 | + |
| 303 | + layer( |
| 304 | + data = data, |
| 305 | + mapping = mapping, |
| 306 | + stat = stat, |
| 307 | + geom = GeomLabel, |
| 308 | + position = position, |
| 309 | + show.legend = show.legend, |
| 310 | + inherit.aes = inherit.aes, |
| 311 | + params = list( |
| 312 | + parse = parse, |
| 313 | + label.padding = label.padding, |
| 314 | + label.r = label.r, |
| 315 | + label.size = label.size, |
| 316 | + na.rm = na.rm, |
| 317 | + fun.geometry = fun.geometry, |
| 318 | + ... |
| 319 | + ) |
| 320 | + ) |
| 321 | +} |
| 322 | + |
| 323 | +#' @export |
| 324 | +#' @rdname ggsf |
| 325 | +#' @inheritParams geom_text |
| 326 | +#' @inheritParams stat_sf_coordinates |
| 327 | +geom_sf_text <- function(mapping = aes(), data = NULL, |
| 328 | + stat = "sf_coordinates", position = "identity", |
| 329 | + ..., |
| 330 | + parse = FALSE, |
| 331 | + nudge_x = 0, |
| 332 | + nudge_y = 0, |
| 333 | + check_overlap = FALSE, |
| 334 | + na.rm = FALSE, |
| 335 | + show.legend = NA, |
| 336 | + inherit.aes = TRUE, |
| 337 | + fun.geometry = NULL) { |
| 338 | + # Automatically determin name of geometry column |
| 339 | + if (!is.null(data) && is_sf(data)) { |
| 340 | + geometry_col <- attr(data, "sf_column") |
| 341 | + } else { |
| 342 | + geometry_col <- "geometry" |
| 343 | + } |
| 344 | + if (is.null(mapping$geometry)) { |
| 345 | + mapping$geometry <- as.name(geometry_col) |
| 346 | + } |
| 347 | + |
| 348 | + if (!missing(nudge_x) || !missing(nudge_y)) { |
| 349 | + if (!missing(position)) { |
| 350 | + stop("Specify either `position` or `nudge_x`/`nudge_y`", call. = FALSE) |
| 351 | + } |
| 352 | + |
| 353 | + position <- position_nudge(nudge_x, nudge_y) |
| 354 | + } |
| 355 | + |
| 356 | + layer( |
| 357 | + data = data, |
| 358 | + mapping = mapping, |
| 359 | + stat = stat, |
| 360 | + geom = GeomText, |
| 361 | + position = position, |
| 362 | + show.legend = show.legend, |
| 363 | + inherit.aes = inherit.aes, |
| 364 | + params = list( |
| 365 | + parse = parse, |
| 366 | + check_overlap = check_overlap, |
| 367 | + na.rm = na.rm, |
| 368 | + fun.geometry = fun.geometry, |
| 369 | + ... |
| 370 | + ) |
| 371 | + ) |
| 372 | +} |
| 373 | + |
| 374 | + |
260 | 375 | #' @export
|
261 | 376 | scale_type.sfc <- function(x) "identity"
|
262 | 377 |
|
|
0 commit comments