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

quantile vignette example does not work with larger numbers of quantiles #65

Open
nickreich opened this issue Dec 29, 2020 · 3 comments
Labels

Comments

@nickreich
Copy link

I tried to reproduce the quantile vignette example but with 10 quantiles instead of 4. The resulting color scale is mis-ordered and mis-labeled.

library(ggplot2)
library(ggridges)
library(viridis)
#> Loading required package: viridisLite
ggplot(iris, aes(x=Sepal.Length, y=Species, fill = factor(stat(quantile)))) +
  stat_density_ridges(
    geom = "density_ridges_gradient", calc_ecdf = TRUE,
    quantiles = 10, quantile_lines = TRUE
  ) +
  scale_fill_viridis_d(name = "Quantiles")
#> Picking joint bandwidth of 0.181

Created on 2020-12-28 by the reprex package (v0.3.0)

@nickreich
Copy link
Author

For what it's worth, it does work with 9 quantiles (and lower numbers as well):

library(ggplot2)
library(ggridges)
library(viridis)
#> Loading required package: viridisLite
ggplot(iris, aes(x=Sepal.Length, y=Species, fill = factor(stat(quantile)))) +
  stat_density_ridges(
    geom = "density_ridges_gradient", calc_ecdf = TRUE,
    quantiles = 9, quantile_lines = TRUE
  ) +
  scale_fill_viridis_d(name = "Quantiles")
#> Picking joint bandwidth of 0.181

Created on 2020-12-28 by the reprex package (v0.3.0)

@clauswilke
Copy link
Collaborator

I suspect the problem is this line:

df_final$quantile <- factor(df_final$quantile)

We're converting the quantiles into a factor separately for each group, and that will go wrong if some groups don't contain all levels. Solutions would be to explicitly set the levels or to do the factor conversion on the entire resulting data frame, not by group.

@clauswilke clauswilke added the bug label Dec 29, 2020
@clauswilke
Copy link
Collaborator

To do the factor conversion on the entire data frame, we could follow the template of StatDensity2d:
https://github.com/tidyverse/ggplot2/blob/feaa4bc341d1df8663ed478b448754b3bca3a375/R/stat-density-2d.r#L127-L132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants