Skip to content

Commit ccd94e1

Browse files
authored
Implement default crs for non-sf objects in coord_sf(). (#3659)
* Implement default crs for non-sf objects in coord_sf(). * make limits work * cleanup code, write documentation * more accurately specify CRS * handle missing or infinite values in sf_transform_xy(). * fix package build * properly reset bbox at beginning of plot generation * cleanup * check that the coord is of type CoordSf before calling its unique function * scale limit improvements * Register bounding box even for stat_sf_coordinates. Gives better default limits. * finalize handling of limits, improve documentation * unit tests for new coord_sf() features * alternative limit methods * ensure point data is always numeric * expand documentation * delete space * capitalize crs * check against incorrect mapping * update docs * more limits methods * better limits methods * simplify error message * fix error message if scale limits inversion problem * reword warnings and error messages.
1 parent 7f88a56 commit ccd94e1

15 files changed

+1000
-78
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ export(scale_y_sqrt)
592592
export(scale_y_time)
593593
export(sec_axis)
594594
export(set_last_plot)
595+
export(sf_transform_xy)
595596
export(should_stop)
596597
export(stage)
597598
export(standardise_aes_names)

NEWS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ This is a small release focusing on fixing regressions introduced in 3.3.1.
1111

1212
* `annotation_raster()` adds support for native rasters. For large rasters,
1313
native rasters render significantly faster than arrays (@kent37, #3388)
14+
15+
* `coord_sf()` now has an argument `default_crs` that specifies the coordinate
16+
reference system (CRS) for non-sf layers and scale/coord limits. This argument
17+
defaults to the World Geodetic System 1984 (WGS84), which means x and y positions
18+
are interpreted as longitude and latitude. This is a potentially breaking change
19+
for users who use projected coordinates in non-sf layers or in limits. Setting
20+
`default_crs = NULL` recovers the old behavior. Further, authors of extension
21+
packages implementing `stat_sf()`-like functionality are encouraged to look at the
22+
source code of `stat_sf()`'s `compute_group()` function to see how to provide
23+
scale-limit hints to `coord_sf()` (@clauswilke, #3659).
1424

1525
* Facet strips now have dedicated position-dependent theme elements
1626
(`strip.text.x.top`, `strip.text.x.bottom`, `strip.text.y.left`,

R/annotation-map.r

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
11
#' @include geom-map.r
22
NULL
33

4-
#' Annotation: a maps
4+
#' Annotation: a map
55
#'
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).
710
#'
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`.
1115
#' @export
1216
#' @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+
#' )
1525
#'
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+
#' )
2136
#'
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)
2639
#'
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+
#' }}}
3258
annotation_map <- function(map, ...) {
3359
# Get map input into correct form
3460
if (!is.data.frame(map)) {

0 commit comments

Comments
 (0)