Skip to content
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

Fix density calculations for groups with one or two elements #2142

Merged
merged 4 commits into from
Jun 1, 2017

Conversation

karawoo
Copy link
Member

@karawoo karawoo commented May 25, 2017

Fixes #2127.

compute_density() now uses stats::density() to compute the density for two-element groups, resulting in plots that look like this (where group 5 has only two data points):

example_scaled

In the case where there is only a single point in the group (where it doesn't really make sense to calculate density), the group gets dropped with a warning.

## Create some data with a couple very small groups
library("tidyverse")
set.seed(1958)
dat <- tibble(x = c(rnorm(n = 10, mean = 7, sd = 2),
                    rpois(n = 10, lambda = 2),
                    1, 9, 4),
              group = c(rep(c("a", "b"), each = 10), "c", "c", "d"))

## Plot
ggplot(dat, aes(x = x, fill = group)) +
  geom_density(alpha = 0.5)
Warning message:
In compute_density(data$x, data$weight, from = range[1], to = range[2],  :
  Groups with fewer than two data points have been dropped.

dropped_group

There is still a legend element for the dropped group; not sure if that should be kept or not.

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

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

Looks good. No need to change the legend.

(In the future, can you please request a review from me? That ensures it gets on my to do list)

R/stat-density.r Outdated
if (nx < 3) {
# if less than 2 points return data frame of NAs and a warning
if (nx < 2) {
warning("Groups with fewer than two data points have been dropped.")
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 call. = FALSE here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@hadley hadley merged commit eedaa81 into tidyverse:master Jun 1, 2017
@hadley
Copy link
Member

hadley commented Jun 1, 2017

Thanks!

@karawoo karawoo deleted the density-with-few-elements branch June 13, 2017 21:13
@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants