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 5 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
32 changes: 27 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,23 @@ 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")) {
return(fortify(map_obj))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know when maps::map() returns a non-map object? Maybe we should just ignore, or raise an error rather than forwarding to fortify()?

map <- c("county", "france", "italy", "nz", "state", "usa", "world", "world2")
vapply(
  map,
  \(x) class(maps::map(x, region = ".", exact = FALSE, plot = FALSE, fill = TRUE)),
  FUN.VALUE = character(1L)
)
#> county france  italy     nz  state    usa  world world2 
#>  "map"  "map"  "map"  "map"  "map"  "map"  "map"  "map"

Created on 2024-12-10 with reprex v2.1.1

Copy link
Collaborator Author

@teunbrand teunbrand Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user passes the namesonly = TRUE option, the maps::map() function does not return a non-map object.
Currently we throw the default error message, which will continue to be the case with this PR.

x <- maps::map(namesonly = TRUE, plot = FALSE)
str(x)
#>  chr [1:1627] "Aruba" "Afghanistan" "Angola" "Angola:Cabinda" "Anguilla" ...

ggplot2::map_data("world", namesonly = TRUE)
#> Error in `fortify()`:
#> ! `data` must be a <data.frame>, or an object coercible by `fortify()`,
#>   or a valid <data.frame>-like object coercible by `as.data.frame()`.
#> Caused by error in `.prevalidate_data_frame_like_object()`:
#> ! `dim(data)` must return an <integer> of length 2.

Created on 2024-12-10 with reprex v2.1.1

Happy to throw a more custom error message if you think that is more appropriate!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't know this, thanks.

Happy to throw a more custom error message if you think that is more appropriate!

IMHO, throwing an error is preferable because we can avoid unnecessary fortify().

How about something like `maps::map()` didn't return a map object. Did you pass some unusual options to `map_data()`? ? I think this is a corner case, so I don't think it needs to be very user-friendly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, now throwing the following error:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
map_data("world", namesonly = TRUE)
#> Error in `map_data()`:
#> ! `maps::map()` must return an object of type <map>, not a character
#>   vector.
#> ℹ Did you pass the right arguments?

Created on 2024-12-10 with reprex v2.1.1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error looks good to me!


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.

Loading