@@ -106,7 +106,7 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,
106106
107107 isobands <- xyz_to_isobands(data , breaks )
108108 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 ])
110110
111111 path_df $ level <- factor (path_df $ level , levels = names(isobands ))
112112
@@ -197,12 +197,12 @@ isoband_z_matrix <- function(data) {
197197 raster
198198}
199199
200- # ' Convert the output of isoband functions
200+ # ' Convert the output of isolines functions
201201# '
202- # ' @param iso the output of [isoband::isolines()] or [isoband::isobands()]
202+ # ' @param iso the output of [isoband::isolines()]
203203# ' @param group the name of the group
204204# '
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()].
206206# ' @noRd
207207# '
208208iso_to_path <- function (iso , group = 1 ) {
@@ -235,6 +235,45 @@ iso_to_path <- function(iso, group = 1) {
235235 )
236236}
237237
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+
238277# ' Pretty isoband level names
239278# '
240279# ' @param isoband_levels `names()` of an [isoband::isobands()] object.
0 commit comments