Skip to content

Expose argument n from stats::density in the interface of stat_density. #1661

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
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 2.1.0.9000


* `position_stack()` and `position_fill()` now sorts the stacking order so it
matches the order of the grouping. Use level reordering to alter the stacking
order. The default legend and stacking order is now also in line. The default
Expand Down Expand Up @@ -28,6 +29,10 @@

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

* `stat_density` now makes argument `n` of the unterlying function
`stats::density` ("number of equally spaced points at which the
density is to be estimated") accessible. (@hbuschme)

* `x` and `y` scales are now symmetric regarding the list of
aesthetics they accept: `xmin_final`, `xmax_final`, `xlower`,
`xmiddle` and `xupper` are now valid `x` aesthetics.
Expand Down
25 changes: 15 additions & 10 deletions R/stat-density.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#' \code{\link{density}} for details
#' @param kernel kernel used for density estimation, see
#' \code{\link{density}} for details
#' @param n number of equally spaced points at which the density is to be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a note about powers of two being faster? (IIRC)

#' estimated, should be a power of two, see \code{\link{density}} for
#' details
#' @param trim This parameter only matters if you are displaying multiple
#' densities in one plot. If \code{FALSE}, the default, each density is
#' computed on the full range of the data. If \code{TRUE}, each density
Expand All @@ -25,6 +28,7 @@ stat_density <- function(mapping = NULL, data = NULL,
bw = "nrd0",
adjust = 1,
kernel = "gaussian",
n = 512,
trim = FALSE,
na.rm = FALSE,
show.legend = NA,
Expand All @@ -42,6 +46,7 @@ stat_density <- function(mapping = NULL, data = NULL,
bw = bw,
adjust = adjust,
kernel = kernel,
n = n,
trim = trim,
na.rm = na.rm,
...
Expand All @@ -58,45 +63,45 @@ StatDensity <- ggproto("StatDensity", Stat,
default_aes = aes(y = ..density.., fill = NA),

compute_group = function(data, scales, bw = "nrd0", adjust = 1, kernel = "gaussian",
trim = FALSE, na.rm = FALSE) {
n = 512, trim = FALSE, na.rm = FALSE) {
if (trim) {
range <- range(data$x, na.rm = TRUE)
} else {
range <- scales$x$dimension()
}

compute_density(data$x, data$weight, from = range[1], to = range[2],
bw = bw, adjust = adjust, kernel = kernel)
bw = bw, adjust = adjust, kernel = kernel, n = n)
}

)

compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1,
kernel = "gaussian") {
n <- length(x)
kernel = "gaussian", n = 512) {
nx <- length(x)
if (is.null(w)) {
w <- rep(1 / n, n)
w <- rep(1 / nx, nx)
}

# if less than 3 points, spread density evenly over points
if (n < 3) {
if (nx < 3) {
return(data.frame(
x = x,
density = w / sum(w),
scaled = w / max(w),
count = 1,
n = n
n = nx
))
}

dens <- stats::density(x, weights = w, bw = bw, adjust = adjust,
kernel = kernel, from = from, to = to)
kernel = kernel, n = n, from = from, to = to)

data.frame(
x = dens$x,
density = dens$y,
scaled = dens$y / max(dens$y, na.rm = TRUE),
count = dens$y * n,
n = n
count = dens$y * nx,
n = nx
)
}
7 changes: 6 additions & 1 deletion man/geom_density.Rd

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