Skip to content

Commit 0eac37b

Browse files
hbuschmehadley
authored andcommitted
Expose argument n from stats::density in the interface of stat_density. (#1661)
* Expose argument n from stats::density in the interface of stat_density. The argument controls the number of equally spaced points at which the density is to be estimated. As in stats::density the argument n defaults to 512. It is exposed through the function interface of stat_density and can thus also be used from geom_density. An existent local variable n, which holds the number of values in the input vector, has been renamed to nx to avoid a name clash. * Added a note about powers of two being recommended.
1 parent 7881a3d commit 0eac37b

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* The `theme()` constructor now has named arguments rather than ellipsis. This
6060
should make autocomplete substantially more useful.
6161

62+
6263
* `position_stack()` and `position_fill()` now sorts the stacking order so it
6364
matches the order of the grouping. Use level reordering to alter the stacking
6465
order. The default legend and stacking order is now also in line. The default
@@ -87,6 +88,10 @@
8788

8889
* `stat_ecdf()` respects `pad` argument (#1646).
8990

91+
* `stat_density` now makes argument `n` of the unterlying function
92+
`stats::density` ("number of equally spaced points at which the
93+
density is to be estimated") accessible. (@hbuschme)
94+
9095
* `x` and `y` scales are now symmetric regarding the list of
9196
aesthetics they accept: `xmin_final`, `xmax_final`, `xlower`,
9297
`xmiddle` and `xupper` are now valid `x` aesthetics.

R/stat-density.r

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#' \code{\link{density}} for details
55
#' @param kernel kernel used for density estimation, see
66
#' \code{\link{density}} for details
7+
#' @param n number of equally spaced points at which the density is to be
8+
#' estimated, should be a power of two, see \code{\link{density}} for
9+
#' details
710
#' @param trim This parameter only matters if you are displaying multiple
811
#' densities in one plot. If \code{FALSE}, the default, each density is
912
#' computed on the full range of the data. If \code{TRUE}, each density
@@ -25,6 +28,7 @@ stat_density <- function(mapping = NULL, data = NULL,
2528
bw = "nrd0",
2629
adjust = 1,
2730
kernel = "gaussian",
31+
n = 512,
2832
trim = FALSE,
2933
na.rm = FALSE,
3034
show.legend = NA,
@@ -42,6 +46,7 @@ stat_density <- function(mapping = NULL, data = NULL,
4246
bw = bw,
4347
adjust = adjust,
4448
kernel = kernel,
49+
n = n,
4550
trim = trim,
4651
na.rm = na.rm,
4752
...
@@ -58,45 +63,45 @@ StatDensity <- ggproto("StatDensity", Stat,
5863
default_aes = aes(y = ..density.., fill = NA),
5964

6065
compute_group = function(data, scales, bw = "nrd0", adjust = 1, kernel = "gaussian",
61-
trim = FALSE, na.rm = FALSE) {
66+
n = 512, trim = FALSE, na.rm = FALSE) {
6267
if (trim) {
6368
range <- range(data$x, na.rm = TRUE)
6469
} else {
6570
range <- scales$x$dimension()
6671
}
6772

6873
compute_density(data$x, data$weight, from = range[1], to = range[2],
69-
bw = bw, adjust = adjust, kernel = kernel)
74+
bw = bw, adjust = adjust, kernel = kernel, n = n)
7075
}
7176

7277
)
7378

7479
compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1,
75-
kernel = "gaussian") {
76-
n <- length(x)
80+
kernel = "gaussian", n = 512) {
81+
nx <- length(x)
7782
if (is.null(w)) {
78-
w <- rep(1 / n, n)
83+
w <- rep(1 / nx, nx)
7984
}
8085

8186
# if less than 3 points, spread density evenly over points
82-
if (n < 3) {
87+
if (nx < 3) {
8388
return(data.frame(
8489
x = x,
8590
density = w / sum(w),
8691
scaled = w / max(w),
8792
count = 1,
88-
n = n
93+
n = nx
8994
))
9095
}
9196

9297
dens <- stats::density(x, weights = w, bw = bw, adjust = adjust,
93-
kernel = kernel, from = from, to = to)
98+
kernel = kernel, n = n, from = from, to = to)
9499

95100
data.frame(
96101
x = dens$x,
97102
density = dens$y,
98103
scaled = dens$y / max(dens$y, na.rm = TRUE),
99-
count = dens$y * n,
100-
n = n
104+
count = dens$y * nx,
105+
n = nx
101106
)
102107
}

man/geom_density.Rd

Lines changed: 6 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)