Skip to content

Commit 54a2f2f

Browse files
authored
Make sure nbins are converted to the correct binwidth (#3704)
1 parent a48fb5b commit 54a2f2f

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

R/stat-contour.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,
102102
compute_group = function(data, scales, bins = NULL, binwidth = NULL, breaks = NULL, na.rm = FALSE) {
103103

104104
z_range <- range(data$z, na.rm = TRUE, finite = TRUE)
105-
breaks <- contour_breaks(z_range, bins, binwidth)
105+
breaks <- contour_breaks(z_range, bins, binwidth, breaks)
106106

107107
isobands <- xyz_to_isobands(data, breaks)
108108
names(isobands) <- pretty_isoband_levels(names(isobands))
@@ -134,7 +134,7 @@ contour_breaks <- function(z_range, bins = NULL, binwidth = NULL, breaks = NULL)
134134

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

140140
# If necessary, compute breaks from binwidth

tests/testthat/test-stat-contour.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_that("contour breaks can be set manually and by bins and binwidth", {
1717
range <- c(0, 1)
1818
expect_equal(contour_breaks(range), pretty(range, 10))
1919
expect_identical(contour_breaks(range, breaks = 1:3), 1:3)
20-
expect_length(contour_breaks(range, bins = 5), 6)
20+
expect_length(contour_breaks(range, bins = 5), 5)
2121
expect_equal(resolution(contour_breaks(range, binwidth = 0.3)), 0.3)
2222
})
2323

0 commit comments

Comments
 (0)