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# '
2427# ' geom_polygon(aes(group = group), colour = "white")
2528# ' }
2629fortify.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, ...) {
8086map_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
0 commit comments