@@ -106,7 +106,7 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,
106
106
107
107
isobands <- xyz_to_isobands(data , breaks )
108
108
names(isobands ) <- pretty_isoband_levels(names(isobands ))
109
- path_df <- iso_to_path (isobands , data $ group [1 ])
109
+ path_df <- iso_to_polygon (isobands , data $ group [1 ])
110
110
111
111
path_df $ level <- factor (path_df $ level , levels = names(isobands ))
112
112
@@ -197,12 +197,12 @@ isoband_z_matrix <- function(data) {
197
197
raster
198
198
}
199
199
200
- # ' Convert the output of isoband functions
200
+ # ' Convert the output of isolines functions
201
201
# '
202
- # ' @param iso the output of [isoband::isolines()] or [isoband::isobands()]
202
+ # ' @param iso the output of [isoband::isolines()]
203
203
# ' @param group the name of the group
204
204
# '
205
- # ' @return A data frame that can be passed to [geom_path()] or [geom_polygon()] .
205
+ # ' @return A data frame that can be passed to [geom_path()].
206
206
# ' @noRd
207
207
# '
208
208
iso_to_path <- function (iso , group = 1 ) {
@@ -235,6 +235,45 @@ iso_to_path <- function(iso, group = 1) {
235
235
)
236
236
}
237
237
238
+ # ' Convert the output of isoband functions
239
+ # '
240
+ # ' @param iso the output of [isoband::isobands()]
241
+ # ' @param group the name of the group
242
+ # '
243
+ # ' @return A data frame that can be passed to [geom_polygon()].
244
+ # ' @noRd
245
+ # '
246
+ iso_to_polygon <- function (iso , group = 1 ) {
247
+ lengths <- vapply(iso , function (x ) length(x $ x ), integer(1 ))
248
+
249
+ if (all(lengths == 0 )) {
250
+ warn(" stat_contour(): Zero contours were generated" )
251
+ return (new_data_frame())
252
+ }
253
+
254
+ levels <- names(iso )
255
+ xs <- unlist(lapply(iso , " [[" , " x" ), use.names = FALSE )
256
+ ys <- unlist(lapply(iso , " [[" , " y" ), use.names = FALSE )
257
+ ids <- unlist(lapply(iso , " [[" , " id" ), use.names = FALSE )
258
+ item_id <- rep(seq_along(iso ), lengths )
259
+
260
+ # Add leading zeros so that groups can be properly sorted
261
+ groups <- paste(group , sprintf(" %03d" , item_id ), sep = " -" )
262
+ groups <- factor (groups )
263
+
264
+ new_data_frame(
265
+ list (
266
+ level = rep(levels , lengths ),
267
+ x = xs ,
268
+ y = ys ,
269
+ piece = as.integer(groups ),
270
+ group = groups ,
271
+ subgroup = ids
272
+ ),
273
+ n = length(xs )
274
+ )
275
+ }
276
+
238
277
# ' Pretty isoband level names
239
278
# '
240
279
# ' @param isoband_levels `names()` of an [isoband::isobands()] object.
0 commit comments