Skip to content

Proper number of bins in stat_contour() #3976

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 2 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* `stat_function()` now works with transformed y axes, e.g. `scale_y_log10()`
(@clauswilke, #3905).

* A bug was fixed in `stat_contour()` when calculating breaks based on
the `bins` argument (@clauswilke, #3879).

* A newly added geom `geom_density_2d_filled()` and associated stat
`stat_density_2d_filled()` can draw filled density contours
(@clauswilke, #3846).
Expand Down
2 changes: 1 addition & 1 deletion R/geom-contour.r
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#' v + geom_contour_filled()
#'
#' # Setting bins creates evenly spaced contours in the range of the data
#' v + geom_contour(bins = 3)
#' v + geom_contour(bins = 5)
#' v + geom_contour(bins = 10)
#'
#' # Setting binwidth does the same thing, parameterised by the distance
#' # between contours
Expand Down
17 changes: 12 additions & 5 deletions R/stat-contour.r
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,26 @@ contour_breaks <- function(z_range, bins = NULL, binwidth = NULL, breaks = NULL)
# If no parameters set, use pretty bins
if (is.null(bins) && is.null(binwidth)) {
breaks <- pretty(z_range, 10)
return(breaks)
}

# If provided, use bins to calculate binwidth
if (!is.null(bins)) {
binwidth <- diff(z_range) / (bins - 1)
}

# If necessary, compute breaks from binwidth
if (is.null(breaks)) {
breaks <- fullseq(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)
}

return(breaks)
}

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

#' Compute isoband objects
Expand Down
2 changes: 1 addition & 1 deletion man/geom_contour.Rd

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

2 changes: 1 addition & 1 deletion man/ggplot2-package.Rd

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

4 changes: 2 additions & 2 deletions man/scale_discrete.Rd

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

4 changes: 2 additions & 2 deletions man/scale_manual.Rd

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

4 changes: 2 additions & 2 deletions man/scale_shape.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
Expand Up @@ -36,7 +36,9 @@ test_that("contour breaks can be set manually and by bins and binwidth", {
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), 5)
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)
})

Expand Down