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

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

merged 2 commits into from
Sep 23, 2016

Conversation

hbuschme
Copy link
Contributor

@hbuschme hbuschme commented Jul 6, 2016

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.

I needed this argument because plots created with geom_density contained a large number of points (1024?) per density curve. This resulted in large file sizes when saving the plot as a vector graphic. I think that it is generally useful to be able to control this parameter of the density function.

I rebuild the documentation, and tested the new functionality. However, I added a new argument to an existing function and – not being an R expert – I am not sure whether this might break someones code. The argument has a default value, but I did not add it at the very end of the argument list but rather at a point where it seemed fitting.

@codecov-io
Copy link

Current coverage is 65.53%

Merging #1661 into master will increase coverage by <.01%

@@             master      #1661   diff @@
==========================================
  Files           158        158          
  Lines          5547       5548     +1   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           3635       3636     +1   
  Misses         1912       1912          
  Partials          0          0          

Powered by Codecov. Last updated by b181e9a...d0acecd

@@ -4,6 +4,8 @@
#' \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)

@hadley
Copy link
Member

hadley commented Jul 28, 2016

This looks reasonable. Would you mind including a couple of plots in this PR just to verify that is works? (i.e. picking a really extreme n)

@hadley hadley added in progress feature a feature request or enhancement labels Jul 28, 2016
@hadley hadley added this to the v2.2.0 milestone Jul 28, 2016
@hbuschme
Copy link
Contributor Author

hbuschme commented Aug 8, 2016

I have added a note about powers of two being recommended – though n is rounded to the nearest power of two automatically if n > 512.

Here are the requested plots (for n = 512, 64, 16, and 4).

data(tips, package = "reshape")
ggplot(tips, aes(x=tip)) + geom_density(stat=StatDensity, n=512)

ggplot(tips, aes(x=tip)) + geom_density(stat=StatDensity, n=512)

ggplot(tips, aes(x=tip)) + geom_density(stat=StatDensity, n=64)

ggplot(tips, aes(x=tip)) + geom_density(stat=StatDensity, n=64)

ggplot(tips, aes(x=tip)) + geom_density(stat=StatDensity, n=16)

ggplot(tips, aes(x=tip)) + geom_density(stat=StatDensity, n=16)

ggplot(tips, aes(x=tip)) + geom_density(stat=StatDensity, n=4)

ggplot(tips, aes(x=tip)) + geom_density(stat=StatDensity, n=4)

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.
@hadley hadley merged commit 0eac37b into tidyverse:master Sep 23, 2016
@hadley
Copy link
Member

hadley commented Sep 23, 2016

Thanks!

@hbuschme hbuschme deleted the expose-n-density-estimates branch September 23, 2016 23:02
@hbuschme hbuschme restored the expose-n-density-estimates branch September 23, 2016 23:06
@hbuschme hbuschme deleted the expose-n-density-estimates branch September 23, 2016 23:07
@lock
Copy link

lock bot commented Jan 18, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants