Skip to content

Allows breaks to be a function in geom_contour() #4652

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
Nov 9, 2021
Merged
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
@@ -5,6 +5,8 @@
all `values` on the legend instead, use
`scale_*_manual(values = vals, limits = names(vals))`. (@teunbrand, @banfai,
#4511, #4534)

* `geom_contour()` now accepts a function in the `breaks` argument (@eliocamp, #4652).

# ggplot2 3.3.5
This is a very small release focusing on fixing a couple of untenable issues
11 changes: 8 additions & 3 deletions R/geom-contour.r
Original file line number Diff line number Diff line change
@@ -19,9 +19,14 @@
#' @inheritParams geom_path
#' @param bins Number of contour bins. Overridden by `binwidth`.
#' @param binwidth The width of the contour bins. Overridden by `breaks`.
#' @param breaks Numeric vector to set the contour breaks. Overrides `binwidth`
#' and `bins`. By default, this is a vector of length ten with [pretty()]
#' breaks.
#' @param breaks One of:
#' - Numeric vector to set the contour breaks
#' - A function that takes the range of the data and binwidth as input
#' and returns breaks as output. A function can be created from a formula
#' (e.g. ~ fullseq(.x, .y)).
#'
#' Overrides `binwidth` and `bins`. By default, this is a vector of length
#' ten with [pretty()] breaks.
#' @seealso [geom_density_2d()]: 2d density contours
#' @export
#' @examples
15 changes: 11 additions & 4 deletions R/stat-contour.r
Original file line number Diff line number Diff line change
@@ -144,10 +144,17 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,
#' @noRd
#'
contour_breaks <- function(z_range, bins = NULL, binwidth = NULL, breaks = NULL) {
if (!is.null(breaks)) {
breaks <- allow_lambda(breaks)

if (is.numeric(breaks)) {
return(breaks)
}

breaks_fun <- fullseq
if (is.function(breaks)) {
breaks_fun <- breaks
}

# If no parameters set, use pretty bins
if (is.null(bins) && is.null(binwidth)) {
breaks <- pretty(z_range, 10)
@@ -167,20 +174,20 @@ contour_breaks <- function(z_range, bins = NULL, binwidth = NULL, breaks = NULL)
}

binwidth <- diff(z_range) / (bins - 1)
breaks <- fullseq(z_range, binwidth)
breaks <- breaks_fun(z_range, binwidth)

# Sometimes the above sequence yields one bin too few.
# If this happens, try again.
if (length(breaks) < bins + 1) {
binwidth <- diff(z_range) / bins
breaks <- fullseq(z_range, binwidth)
breaks <- breaks_fun(z_range, binwidth)
}

return(breaks)
}

# if we haven't returned yet, compute breaks from binwidth
fullseq(z_range, binwidth)
breaks_fun(z_range, binwidth)
}

#' Compute isoband objects
13 changes: 10 additions & 3 deletions man/geom_contour.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions man/geom_density_2d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/testthat/test-stat-contour.R
Original file line number Diff line number Diff line change
@@ -32,14 +32,16 @@ test_that("contouring irregularly spaced data works", {
expect_setequal(d8$y, c(2, 20/9, 16/9))
})

test_that("contour breaks can be set manually and by bins and binwidth", {
test_that("contour breaks can be set manually and by bins and binwidth and a function", {
range <- c(0, 1)
expect_equal(contour_breaks(range), pretty(range, 10))
expect_identical(contour_breaks(range, breaks = 1:3), 1:3)
expect_length(contour_breaks(range, bins = 5), 6)
# shifting the range by 0.2 hits another execution branch in contour_breaks()
expect_length(contour_breaks(range + 0.2, bins = 5), 6)
expect_equal(resolution(contour_breaks(range, binwidth = 0.3)), 0.3)
expect_equal(contour_breaks(range), contour_breaks(range, breaks = fullseq))
expect_equal(contour_breaks(range), contour_breaks(range, breaks = ~fullseq(.x, .y)))
})

test_that("geom_contour_filled() and stat_contour_filled() result in identical layer data", {