Skip to content

Contour removes duplicates #5243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* `stat_contour()` and `stat_contour_filled()` now warn about and remove
duplicated coordinates (@teunbrand, #5215).
* `annotation_logticks()` skips drawing ticks when the scale range is non-finite
instead of throwing an error (@teunbrand, #5229).
* Fixed spurious warnings when the `weight` was used in `stat_bin_2d()`,
Expand Down
35 changes: 35 additions & 0 deletions R/stat-contour.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ StatContour <- ggproto("StatContour", Stat,
params
},

setup_data = function(data, params) {
contour_deduplicate(data)
},

compute_group = function(data, scales, z.range, bins = NULL, binwidth = NULL,
breaks = NULL, na.rm = FALSE) {

Expand Down Expand Up @@ -129,6 +133,10 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,
params
},

setup_data = function(data, params) {
contour_deduplicate(data)
},

compute_group = function(data, scales, z.range, bins = NULL, binwidth = NULL, breaks = NULL, na.rm = FALSE) {
breaks <- contour_breaks(z.range, bins, binwidth, breaks)

Expand Down Expand Up @@ -352,3 +360,30 @@ pretty_isoband_levels <- function(isoband_levels, dig.lab = 3) {
# and open at their upper boundary
sprintf("(%s, %s]", label_low, label_high)
}

#' De-duplicate data for contours
#'
#' Gives a warning if data has duplicates and throws out duplicated rows.
#'
#' @param data A `data.frame`
#' @param check Column names to check for duplicates
#'
#' @return A de-duplicated `data.frame`
#' @noRd
contour_deduplicate <- function(data, check = c("x", "y", "group", "PANEL")) {
check <- intersect(check, names(data))
if (length(check) == 0) {
return(data)
}
if (vec_duplicate_any(data[, check, drop = FALSE])) {
# We use fromLast here to be consistent with `isoband_z_matrix()` behaviour
dups <- duplicated(data[, check, drop = FALSE], fromLast = TRUE)
data <- data[!dups, , drop = FALSE]

cli::cli_warn(c(
"Contour data has duplicated {.field x}, {.field y} coordinates.",
i = "{sum(dups)} duplicated row{?s} have been dropped."
))
}
data
}
20 changes: 20 additions & 0 deletions tests/testthat/test-stat-contour.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,23 @@ test_that("basic stat_contour_filled() plot builds", {
# implementation in isoband
expect_silent(ggplot_build(p))
})

test_that("stat_contour() removes duplicated coordinates", {

df <- data_frame0(
x = c(1, 1, 2, 2, 1, 1, 2, 2),
y = c(1, 2, 1, 2, 1, 2, 1, 2),
z = c(1, 0, 0, 1, 1, 0, 0, 1),
group = c(1, 1, 1, 1, 2, 2, 2, 2)
)

layer <- stat_contour()

expect_silent(layer$stat$setup_data(df))
expect_warning(
new <- layer$stat$setup_data(transform(df, group = 1)),
"has duplicated"
)
expect_equal(new, df[1:4,], ignore_attr = TRUE)
})