Skip to content

Commit 5971ff4

Browse files
teunbrandthomasp85
andauthored
chore: merge isoband/isolines functions (#6021)
* merge isoband/isolines functions * Update R/stat-contour.R Co-authored-by: Thomas Lin Pedersen <[email protected]> --------- Co-authored-by: Thomas Lin Pedersen <[email protected]>
1 parent e69687a commit 5971ff4

File tree

1 file changed

+12
-41
lines changed

1 file changed

+12
-41
lines changed

R/stat-contour.R

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ StatContour <- ggproto("StatContour", Stat,
108108
breaks <- contour_breaks(z.range, bins, binwidth, breaks)
109109

110110
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")
112112

113113
path_df$level <- as.numeric(path_df$level)
114114
path_df$nlevel <- rescale_max(path_df$level)
@@ -142,7 +142,7 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,
142142

143143
isobands <- withr::with_options(list(OutDec = "."), xyz_to_isobands(data, breaks))
144144
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")
146146

147147
path_df$level <- ordered(path_df$level, levels = names(isobands))
148148
path_df$level_low <- breaks[as.numeric(path_df$level)]
@@ -259,51 +259,17 @@ isoband_z_matrix <- function(data) {
259259
raster
260260
}
261261

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-
298262
#' Convert the output of isoband functions
299263
#'
300-
#' @param iso the output of [isoband::isobands()]
264+
#' @param iso the output of [isoband::isobands()] or [isoband::isolines()]
301265
#' @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.
302268
#'
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()].
304270
#' @noRd
305271
#'
306-
iso_to_polygon <- function(iso, group = 1) {
272+
iso_to_geom <- function(iso, group = 1, geom = "path") {
307273
lengths <- vapply(iso, function(x) length(x$x), integer(1))
308274

309275
if (all(lengths == 0)) {
@@ -319,6 +285,11 @@ iso_to_polygon <- function(iso, group = 1) {
319285

320286
# Add leading zeros so that groups can be properly sorted
321287
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+
322293
groups <- factor(groups)
323294

324295
data_frame0(

0 commit comments

Comments
 (0)