Skip to content

Tweaks to map_data()/fortify.map()` #6218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()"
)

Check warning on line 32 in R/fortify-map.R

View check run for this annotation

Codecov / codecov/patch

R/fortify-map.R#L30-L32

Added lines #L30 - L32 were not covered by tests
df <- data_frame0(
long = model$x,
lat = model$y,
Expand All @@ -46,10 +52,10 @@
#' 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 @@
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)
})
Loading