Closed bjreisman closed 5 years ago
This is just how ggplot2 works. Limits on scales remove the data outside the limits. This usually generates a warning about missing values (see below). You'll have the same issue with all other geoms (e.g., geom_boxplot()
).
library(ggplot2)
library(ggridges)
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
stat_density_ridges(quantile_lines = TRUE, quantiles = 2) +
scale_x_continuous(limits = c(5, 9))
#> Picking joint bandwidth of 0.166
#> Warning: Removed 22 rows containing non-finite values
#> (stat_density_ridges).
Created on 2018-11-30 by the reprex package (v0.2.1)
Ah, thank you!
When scales are limited with
scale_x_continuous(limits = c(min, max))
, only the data within the limits is retained for the quantile calculation. This leads to plotted quantile lines which do not match the true data quantiles.I think this is the expected behavior when setting limits inside the scale function, as opposed to zooming in on a part of the plot. See: stackoverflow thread
While I don't think this is a bug per-se, but it caught me off guard at first and it may be worthwhile to include a warning when trying to use limited axis with quantile lines. Normally I'd be happy to contribute, but I think it would require the quantile lines function to know whether the scales have been limited (or vice-versa) and I'm not sure how to go about that.