Skip to content

Commit 3139895

Browse files
committed
inline fortify.map into map_data()
1 parent 7e95eb0 commit 3139895

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

R/fortify-map.R

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,23 @@ fortify.map <- function(model, data, ...) {
8080
map_data <- function(map, region = ".", exact = FALSE, ...) {
8181
check_installed("maps", reason = "for `map_data()`.")
8282
map_obj <- maps::map(map, region, exact = exact, plot = FALSE, fill = TRUE, ...)
83-
fortify(map_obj)
83+
84+
if (!inherits(map_obj, "map")) {
85+
return(fortify(map_obj))
86+
}
87+
88+
df <- data_frame0(
89+
long = map_obj$x,
90+
lat = map_obj$y,
91+
group = cumsum(is.na(map_obj$x) & is.na(map_obj$y)) + 1,
92+
order = seq_along(map_obj$x),
93+
.size = length(map_obj$x)
94+
)
95+
96+
names <- lapply(strsplit(map_obj$names, "[:,]"), "[", 1:2)
97+
names <- vec_rbind(!!!names, .name_repair = ~ c("region", "subregion"))
98+
df[names(names)] <- vec_slice(names, df$group)
99+
vec_slice(df, stats::complete.cases(df$lat, df$long))
84100
}
85101

86102
#' Create a layer of map borders

0 commit comments

Comments
 (0)