Skip to content

Commit 87d5cc7

Browse files
committed
Access to bw argument of density in stat_density
Makes it easy to get consistent smoothing between facets for example
1 parent c01e626 commit 87d5cc7

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 2.0.0.9000
22

3+
* Add access to `bw` argument of `density` in `stat_density`, which makes
4+
it easy to get consistent smoothing between facets for example (@jiho)
5+
36
## Bug fixes and minor improvements
47

58
* Removed a superfluous comma in `theme-defaults.r` code (@jschoeley)

R/stat-density.r

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#' @param adjust see \code{\link{density}} for details
1+
#' @param bw the smoothing bandwidth to be used, see
2+
#' \code{\link{density}} for details
3+
#' @param adjust adjustment of the bandwidth, see
4+
#' \code{\link{density}} for details
25
#' @param kernel kernel used for density estimation, see
36
#' \code{\link{density}} for details
47
#' @param trim This parameter only matters if you are displaying multiple
@@ -17,7 +20,7 @@
1720
#' @export
1821
#' @rdname geom_density
1922
stat_density <- function(mapping = NULL, data = NULL, geom = "area",
20-
position = "stack", adjust = 1, kernel = "gaussian",
23+
position = "stack", bw = "nrd0", adjust = 1, kernel = "gaussian",
2124
trim = FALSE, na.rm = FALSE,
2225
show.legend = NA, inherit.aes = TRUE, ...) {
2326

@@ -30,6 +33,7 @@ stat_density <- function(mapping = NULL, data = NULL, geom = "area",
3033
show.legend = show.legend,
3134
inherit.aes = inherit.aes,
3235
params = list(
36+
bw = bw,
3337
adjust = adjust,
3438
kernel = kernel,
3539
trim = trim,
@@ -47,7 +51,7 @@ StatDensity <- ggproto("StatDensity", Stat,
4751
required_aes = "x",
4852
default_aes = aes(y = ..density.., fill = NA),
4953

50-
compute_group = function(data, scales, adjust = 1, kernel = "gaussian",
54+
compute_group = function(data, scales, bw = "nrd0", adjust = 1, kernel = "gaussian",
5155
trim = FALSE, na.rm = FALSE) {
5256
if (trim) {
5357
range <- range(data$x, na.rm = TRUE)
@@ -56,7 +60,7 @@ StatDensity <- ggproto("StatDensity", Stat,
5660
}
5761

5862
compute_density(data$x, data$weight, from = range[1], to = range[2],
59-
adjust = adjust, kernel = kernel)
63+
bw = bw, adjust = adjust, kernel = kernel)
6064
}
6165

6266
)

man/geom_density.Rd

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_violin.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)