1
1
# ' Fortify method for map objects
2
2
# '
3
+ # ' @description
4
+ # ' `r lifecycle::badge("deprecated")`
5
+ # '
3
6
# ' This function turns a map into a data frame that can more easily be
4
7
# ' plotted with ggplot2.
5
8
# '
24
27
# ' geom_polygon(aes(group = group), colour = "white")
25
28
# ' }
26
29
fortify.map <- function (model , data , ... ) {
30
+ lifecycle :: deprecate_warn(
31
+ " 3.6.0" , I(" `fortify(<map>)`" ), " map_data()"
32
+ )
27
33
df <- data_frame0(
28
34
long = model $ x ,
29
35
lat = model $ y ,
@@ -46,10 +52,10 @@ fortify.map <- function(model, data, ...) {
46
52
# ' for plotting with ggplot2.
47
53
# '
48
54
# ' @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].
53
59
# ' @param region name(s) of subregion(s) to include. Defaults to `.` which
54
60
# ' includes all subregions. See documentation for [maps::map()]
55
61
# ' for more details.
@@ -80,7 +86,27 @@ fortify.map <- function(model, data, ...) {
80
86
map_data <- function (map , region = " ." , exact = FALSE , ... ) {
81
87
check_installed(" maps" , reason = " for `map_data()`." )
82
88
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 ))
84
110
}
85
111
86
112
# ' Create a layer of map borders
0 commit comments