Skip to content

Commit

Permalink
Better binning algorithm for contours (#4006)
Browse files Browse the repository at this point in the history
* better binning algorithm. closes #4004

* update news

* better results for bins == 1
  • Loading branch information
clauswilke committed May 18, 2020
1 parent 908edc9 commit 37ed660
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion NEWS.md
Expand Up @@ -14,7 +14,7 @@
containing no other data or layers (@clauswilke, #3611, #3905, #3983).

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

* A newly added geom `geom_density_2d_filled()` and associated stat
`stat_density_2d_filled()` can draw filled density contours
Expand Down
10 changes: 10 additions & 0 deletions R/stat-contour.r
Expand Up @@ -156,6 +156,16 @@ contour_breaks <- function(z_range, bins = NULL, binwidth = NULL, breaks = NULL)

# If provided, use bins to calculate binwidth
if (!is.null(bins)) {
# round lower limit down and upper limit up to make sure
# we generate bins that span the data range nicely
accuracy <- signif(diff(z_range), 1)/10
z_range[1] <- floor(z_range[1]/accuracy)*accuracy
z_range[2] <- ceiling(z_range[2]/accuracy)*accuracy

if (bins == 1) {
return(z_range)
}

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

Expand Down

0 comments on commit 37ed660

Please sign in to comment.