Skip to content

Commit

Permalink
Tweaks to map_data()/fortify.map()` (#6218)
Browse files Browse the repository at this point in the history
* link maps as strings

* inline `fortify.map` into `map_data()`

* deprecate `fortify.map`

* add deprecation badges

* document

* throw error instead
  • Loading branch information
teunbrand authored Dec 10, 2024
1 parent 679ff96 commit 4dedb82
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 9 deletions.
36 changes: 31 additions & 5 deletions R/fortify-map.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#' Fortify method for map objects
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' This function turns a map into a data frame that can more easily be
#' plotted with ggplot2.
#'
Expand All @@ -24,6 +27,9 @@
#' geom_polygon(aes(group = group), colour = "white")
#' }
fortify.map <- function(model, data, ...) {
lifecycle::deprecate_warn(
"3.6.0", I("`fortify(<map>)`"), "map_data()"
)
df <- data_frame0(
long = model$x,
lat = model$y,
Expand All @@ -46,10 +52,10 @@ fortify.map <- function(model, data, ...) {
#' for plotting with ggplot2.
#'
#' @param map name of map provided by the \pkg{maps} package. These
#' include [maps::county()], [maps::france()],
#' [maps::italy()], [maps::nz()],
#' [maps::state()], [maps::usa()],
#' [maps::world()], [maps::world2()].
#' include [`"county"`][maps::county], [`"france"`][maps::france],
#' [`"italy"`][maps::italy], [`"nz"`][maps::nz],
#' [`"state"`][maps::state], [`"usa"`][maps::usa],
#' [`"world"`][maps::world], or [`"world2"`][maps::world2].
#' @param region name(s) of subregion(s) to include. Defaults to `.` which
#' includes all subregions. See documentation for [maps::map()]
#' for more details.
Expand Down Expand Up @@ -80,7 +86,27 @@ fortify.map <- function(model, data, ...) {
map_data <- function(map, region = ".", exact = FALSE, ...) {
check_installed("maps", reason = "for `map_data()`.")
map_obj <- maps::map(map, region, exact = exact, plot = FALSE, fill = TRUE, ...)
fortify(map_obj)

if (!inherits(map_obj, "map")) {
cli::cli_abort(c(
"{.fn maps::map} must return an object of type {.cls map}, not \\
{obj_type_friendly(map_obj)}.",
i = "Did you pass the right arguments?"
))
}

df <- data_frame0(
long = map_obj$x,
lat = map_obj$y,
group = cumsum(is.na(map_obj$x) & is.na(map_obj$y)) + 1,
order = seq_along(map_obj$x),
.size = length(map_obj$x)
)

names <- lapply(strsplit(map_obj$names, "[:,]"), "[", 1:2)
names <- vec_rbind(!!!names, .name_repair = ~ c("region", "subregion"))
df[names(names)] <- vec_slice(names, df$group)
vec_slice(df, stats::complete.cases(df$lat, df$long))
}

#' Create a layer of map borders
Expand Down
3 changes: 3 additions & 0 deletions R/fortify-spatial.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#' Fortify method for classes from the sp package.
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' To figure out the correct variable name for region, inspect
#' `as.data.frame(model)`.
#'
Expand Down
2 changes: 2 additions & 0 deletions man/fortify.map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/fortify.sp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/map_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/geom-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@

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

# map_data() checks it input

Code
map_data("world", namesonly = TRUE)
Condition
Error in `map_data()`:
! `maps::map()` must return an object of type <map>, not a character vector.
i Did you pass the right arguments?

5 changes: 5 additions & 0 deletions tests/testthat/test-geom-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ test_that("geom_map() checks its input", {
expect_snapshot_error(geom_map(map = letters))
expect_snapshot_error(geom_map(map = mtcars))
})

test_that("map_data() checks it input", {
skip_if_not_installed("maps")
expect_snapshot(map_data("world", namesonly = TRUE), error = TRUE)
})

0 comments on commit 4dedb82

Please sign in to comment.