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

geom_density with groups of 1 or 2 elements #2127

Closed
LSanselme opened this issue May 11, 2017 · 3 comments
Closed

geom_density with groups of 1 or 2 elements #2127

LSanselme opened this issue May 11, 2017 · 3 comments

Comments

@LSanselme
Copy link

@LSanselme LSanselme commented May 11, 2017

Hello,

I am struggling with geom_density when groups consist on fewer than 3 elements.
For a group of 1 element, the density plot consists on a single line of length 1. For a group of 2 elements, this is a rectangle of height 0.5.
Since this does not depends on the y scale, the interpretation of the area under the curves breaks down. This problem becomes obvious when changing coordinates on the x axis (eg unit conversion).

This reproducible example illustrates my point.
This is the density of the measurements of Speed, for the 5 experiments. Each consists on 20 runs.

library(tidyverse)
library(MASS)

michelson %>%
  ggplot(aes(x = Speed, fill = Expt)) +
  geom_density(alpha = 0.5)

image

The density of the measurements, for the 5 experiments when we re-normalize the speed. As expected, the only changes are in the axis texts.

michelson %>%
  mutate(Speed = Speed / mean(Speed)) %>%
  ggplot(aes(x = Speed, fill = Expt)) +
  geom_density(alpha = 0.5)

image

The problem arises when the groups are small,
so we modify the data for the last group to consist on only two runs.

michelson_modif <- michelson
michelson_modif$Expt[83:100] <- 2

michelson_modif %>%
  ggplot(aes(x = Speed, fill = Expt)) +
  geom_density(alpha = 0.5)

image

michelson_modif %>%
  mutate(Speed = Speed / mean(Speed)) %>%
  ggplot(aes(x = Speed, fill = Expt)) +
  geom_density(alpha = 0.5)

image

The two graphs are no longer similar.

Do I miss something here? Is it possible to force the behaviour of geom_density to be similar (ie smooth curves) for all the groups, regardless of the number of elements in them?

Thanks a lot!

@LSanselme
Copy link
Author

@LSanselme LSanselme commented May 11, 2017

This might be linked to the closed issue #595 discussed between @wch and @hadley, or to the closed issue #676.

@hadley
Copy link
Member

@hadley hadley commented May 24, 2017

It looks like I messed up this special case in compute_density():

  if (nx < 3) {
    return(data.frame(
      x = x,
      density = w / sum(w),
      scaled = w / max(w),
      count = 1,
      n = nx
    ))
  }

@LSanselme
Copy link
Author

@LSanselme LSanselme commented Jun 1, 2017

Thanks @karawoo!

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants