@@ -108,7 +108,7 @@ StatContour <- ggproto("StatContour", Stat,
108
108
breaks <- contour_breaks(z.range , bins , binwidth , breaks )
109
109
110
110
isolines <- withr :: with_options(list (OutDec = " ." ), xyz_to_isolines(data , breaks ))
111
- path_df <- iso_to_path (isolines , data $ group [1 ])
111
+ path_df <- iso_to_geom (isolines , data $ group [1 ], geom = " path " )
112
112
113
113
path_df $ level <- as.numeric(path_df $ level )
114
114
path_df $ nlevel <- rescale_max(path_df $ level )
@@ -142,7 +142,7 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,
142
142
143
143
isobands <- withr :: with_options(list (OutDec = " ." ), xyz_to_isobands(data , breaks ))
144
144
names(isobands ) <- pretty_isoband_levels(names(isobands ))
145
- path_df <- iso_to_polygon (isobands , data $ group [1 ])
145
+ path_df <- iso_to_geom (isobands , data $ group [1 ], geom = " polygon " )
146
146
147
147
path_df $ level <- ordered(path_df $ level , levels = names(isobands ))
148
148
path_df $ level_low <- breaks [as.numeric(path_df $ level )]
@@ -259,51 +259,17 @@ isoband_z_matrix <- function(data) {
259
259
raster
260
260
}
261
261
262
- # ' Convert the output of isolines functions
263
- # '
264
- # ' @param iso the output of [isoband::isolines()]
265
- # ' @param group the name of the group
266
- # '
267
- # ' @return A data frame that can be passed to [geom_path()].
268
- # ' @noRd
269
- # '
270
- iso_to_path <- function (iso , group = 1 ) {
271
- lengths <- vapply(iso , function (x ) length(x $ x ), integer(1 ))
272
-
273
- if (all(lengths == 0 )) {
274
- cli :: cli_warn(" {.fn stat_contour}: Zero contours were generated" )
275
- return (data_frame0())
276
- }
277
-
278
- levels <- names(iso )
279
- xs <- unlist(lapply(iso , " [[" , " x" ), use.names = FALSE )
280
- ys <- unlist(lapply(iso , " [[" , " y" ), use.names = FALSE )
281
- ids <- unlist(lapply(iso , " [[" , " id" ), use.names = FALSE )
282
- item_id <- rep(seq_along(iso ), lengths )
283
-
284
- # Add leading zeros so that groups can be properly sorted
285
- groups <- paste(group , sprintf(" %03d" , item_id ), sprintf(" %03d" , ids ), sep = " -" )
286
- groups <- factor (groups )
287
-
288
- data_frame0(
289
- level = rep(levels , lengths ),
290
- x = xs ,
291
- y = ys ,
292
- piece = as.integer(groups ),
293
- group = groups ,
294
- .size = length(xs )
295
- )
296
- }
297
-
298
262
# ' Convert the output of isoband functions
299
263
# '
300
- # ' @param iso the output of [isoband::isobands()]
264
+ # ' @param iso the output of [isoband::isobands()] or [isoband::isolines()]
301
265
# ' @param group the name of the group
266
+ # ' @param geom The type of geometry to return. Either `"path"` or `"polygon"`
267
+ # ' for isolines and isobands respectively.
302
268
# '
303
- # ' @return A data frame that can be passed to [geom_polygon()].
269
+ # ' @return A data frame that can be passed to [geom_polygon()] or [geom_path()] .
304
270
# ' @noRd
305
271
# '
306
- iso_to_polygon <- function (iso , group = 1 ) {
272
+ iso_to_geom <- function (iso , group = 1 , geom = " path " ) {
307
273
lengths <- vapply(iso , function (x ) length(x $ x ), integer(1 ))
308
274
309
275
if (all(lengths == 0 )) {
@@ -319,6 +285,11 @@ iso_to_polygon <- function(iso, group = 1) {
319
285
320
286
# Add leading zeros so that groups can be properly sorted
321
287
groups <- paste(group , sprintf(" %03d" , item_id ), sep = " -" )
288
+ if (geom == " path" ) {
289
+ groups <- paste(groups , sprintf(" %03d" , ids ), sep = " -" )
290
+ ids <- NULL
291
+ }
292
+
322
293
groups <- factor (groups )
323
294
324
295
data_frame0(
0 commit comments