Skip to content

Commit 4dedb82

Browse files
authored
Tweaks to map_data()/fortify.map()` (#6218)
* link maps as strings * inline `fortify.map` into `map_data()` * deprecate `fortify.map` * add deprecation badges * document * throw error instead
1 parent 679ff96 commit 4dedb82

File tree

7 files changed

+56
-9
lines changed

7 files changed

+56
-9
lines changed

R/fortify-map.R

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#' Fortify method for map objects
22
#'
3+
#' @description
4+
#' `r lifecycle::badge("deprecated")`
5+
#'
36
#' This function turns a map into a data frame that can more easily be
47
#' plotted with ggplot2.
58
#'
@@ -24,6 +27,9 @@
2427
#' geom_polygon(aes(group = group), colour = "white")
2528
#' }
2629
fortify.map <- function(model, data, ...) {
30+
lifecycle::deprecate_warn(
31+
"3.6.0", I("`fortify(<map>)`"), "map_data()"
32+
)
2733
df <- data_frame0(
2834
long = model$x,
2935
lat = model$y,
@@ -46,10 +52,10 @@ fortify.map <- function(model, data, ...) {
4652
#' for plotting with ggplot2.
4753
#'
4854
#' @param map name of map provided by the \pkg{maps} package. These
49-
#' include [maps::county()], [maps::france()],
50-
#' [maps::italy()], [maps::nz()],
51-
#' [maps::state()], [maps::usa()],
52-
#' [maps::world()], [maps::world2()].
55+
#' include [`"county"`][maps::county], [`"france"`][maps::france],
56+
#' [`"italy"`][maps::italy], [`"nz"`][maps::nz],
57+
#' [`"state"`][maps::state], [`"usa"`][maps::usa],
58+
#' [`"world"`][maps::world], or [`"world2"`][maps::world2].
5359
#' @param region name(s) of subregion(s) to include. Defaults to `.` which
5460
#' includes all subregions. See documentation for [maps::map()]
5561
#' for more details.
@@ -80,7 +86,27 @@ fortify.map <- function(model, data, ...) {
8086
map_data <- function(map, region = ".", exact = FALSE, ...) {
8187
check_installed("maps", reason = "for `map_data()`.")
8288
map_obj <- maps::map(map, region, exact = exact, plot = FALSE, fill = TRUE, ...)
83-
fortify(map_obj)
89+
90+
if (!inherits(map_obj, "map")) {
91+
cli::cli_abort(c(
92+
"{.fn maps::map} must return an object of type {.cls map}, not \\
93+
{obj_type_friendly(map_obj)}.",
94+
i = "Did you pass the right arguments?"
95+
))
96+
}
97+
98+
df <- data_frame0(
99+
long = map_obj$x,
100+
lat = map_obj$y,
101+
group = cumsum(is.na(map_obj$x) & is.na(map_obj$y)) + 1,
102+
order = seq_along(map_obj$x),
103+
.size = length(map_obj$x)
104+
)
105+
106+
names <- lapply(strsplit(map_obj$names, "[:,]"), "[", 1:2)
107+
names <- vec_rbind(!!!names, .name_repair = ~ c("region", "subregion"))
108+
df[names(names)] <- vec_slice(names, df$group)
109+
vec_slice(df, stats::complete.cases(df$lat, df$long))
84110
}
85111

86112
#' Create a layer of map borders

R/fortify-spatial.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#' Fortify method for classes from the sp package.
22
#'
3+
#' @description
4+
#' `r lifecycle::badge("deprecated")`
5+
#'
36
#' To figure out the correct variable name for region, inspect
47
#' `as.data.frame(model)`.
58
#'

man/fortify.map.Rd

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

man/fortify.sp.Rd

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

man/map_data.Rd

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

tests/testthat/_snaps/geom-map.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@
66

77
`map` must have the columns `x`, `y`, and `id`.
88

9+
# map_data() checks it input
10+
11+
Code
12+
map_data("world", namesonly = TRUE)
13+
Condition
14+
Error in `map_data()`:
15+
! `maps::map()` must return an object of type <map>, not a character vector.
16+
i Did you pass the right arguments?
17+

tests/testthat/test-geom-map.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ test_that("geom_map() checks its input", {
22
expect_snapshot_error(geom_map(map = letters))
33
expect_snapshot_error(geom_map(map = mtcars))
44
})
5+
6+
test_that("map_data() checks it input", {
7+
skip_if_not_installed("maps")
8+
expect_snapshot(map_data("world", namesonly = TRUE), error = TRUE)
9+
})

0 commit comments

Comments
 (0)