Skip to content

stat_ecdf() responds to scale transform #5115

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 4 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# ggplot2 (development version)

* Renamed computed aesthetic in `stat_ecdf()` to `ecdf`, to prevent incorrect
scale transformations (@teunbrand, #5113 and #5112).
* Fixed misbehaviour of `draw_key_boxplot()` and `draw_key_crossbar()` with
skewed key aspect ratio (@teunbrand, #5082).
* `scale_*_binned()` handles zero-range limits more gracefully (@teunbrand,
#5066)
* Binned scales are now compatible with `trans = "date"` and `trans = "time"`
(@teunbrand, #4217).
* `ggsave()` warns when multiple `filename`s are given, and only writes to the
first file (@teunbrand, #5114).
first file (@teunbrand, #5114)
* Fixed a regression in `geom_hex()` where aesthetics were replicated across
bins (@thomasp85, #5037 and #5044)
* Fixed spurious warning when `weight` aesthetic was used in `stat_smooth()`
Expand Down
6 changes: 3 additions & 3 deletions R/stat-ecdf.r
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#' @param pad If `TRUE`, pad the ecdf with additional points (-Inf, 0)
#' and (Inf, 1)
#' @eval rd_computed_vars(
#' y = "Cumulative density corresponding to `x`."
#' ecdf = "Cumulative density corresponding to `x`."
#' )
#' @export
#' @examples
Expand Down Expand Up @@ -73,7 +73,7 @@ stat_ecdf <- function(mapping = NULL, data = NULL,
StatEcdf <- ggproto("StatEcdf", Stat,
required_aes = c("x|y"),

default_aes = aes(y = after_stat(y)),
default_aes = aes(x = after_stat(ecdf), y = after_stat(ecdf)),

setup_params = function(self, data, params) {
params$flipped_aes <- has_flipped_aes(data, params, main_is_orthogonal = FALSE, main_is_continuous = TRUE)
Expand Down Expand Up @@ -103,7 +103,7 @@ StatEcdf <- ggproto("StatEcdf", Stat,

df_ecdf <- data_frame0(
x = x,
y = data_ecdf,
ecdf = data_ecdf,
.size = length(x)
)
df_ecdf$flipped_aes <- flipped_aes
Expand Down
2 changes: 1 addition & 1 deletion man/stat_ecdf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test-stat-ecdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ test_that("stat_ecdf works in both directions", {
expect_snapshot_error(ggplot_build(p))
})

# See #5113 and #5112
test_that("stat_ecdf responds to axis transformations", {
n <- 4
answer <- c(seq(0, 1, length.out = n + 1), 1)
p <- ggplot(data_frame0(x = seq_len(n)), aes(x)) + stat_ecdf()

ld <- layer_data(p)
expect_equal(ld$y, answer)

ld <- layer_data(p + scale_y_sqrt())
expect_equal(ld$y, sqrt(answer))
})