Skip to content

Fix stat_function() for transformed scales #3908

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 2 commits into from
Apr 30, 2020
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* `annotation_raster()` adds support for native rasters. For large rasters,
native rasters render significantly faster than arrays (@kent37, #3388)

* `stat_function()` now works with transformed y axes, e.g. `scale_y_log10()`
(@clauswilke, #3905).

* A newly added geom `geom_density_2d_filled()` and associated stat
`stat_density_2d_filled()` can draw filled density contours
(@clauswilke, #3846).
Expand Down
10 changes: 8 additions & 2 deletions R/stat-function.r
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ stat_function <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
StatFunction <- ggproto("StatFunction", Stat,
default_aes = aes(y = after_stat(y)),
default_aes = aes(y = after_scale(y)),

compute_group = function(data, scales, fun, xlim = NULL, n = 101, args = list()) {
range <- xlim %||% scales$x$dimension()
Expand All @@ -113,9 +113,15 @@ StatFunction <- ggproto("StatFunction", Stat,

if (is.formula(fun)) fun <- as_function(fun)

y_out <- do.call(fun, c(list(quote(x_trans)), args))
if (!is.null(scales$y) && !scales$y$is_discrete()) {
# For continuous scales, need to apply transform
y_out <- scales$y$trans$transform(y_out)
}

new_data_frame(list(
x = xseq,
y = do.call(fun, c(list(quote(x_trans)), args))
y = y_out
))
}
)
45 changes: 45 additions & 0 deletions tests/testthat/test-stats-function.r
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,51 @@ test_that("works with discrete x", {
expect_equal(ret$y, 1:2)
})

test_that("works with transformed scales", {
dat <- data_frame(x = 1:10, y = (1:10)^2)

# first without explicit mapping of y
base <- ggplot(dat, aes(x, group = 1)) +
stat_function(fun = ~ .x^2, n = 5)

ret <- layer_data(base)
expect_equal(nrow(ret), 5)
expect_equal(ret$y, ret$x^2)

ret <- layer_data(base + scale_x_log10())
expect_equal(nrow(ret), 5)
expect_equal(ret$y, (10^ret$x)^2)

ret <- layer_data(base + scale_y_log10())
expect_equal(nrow(ret), 5)
expect_equal(10^ret$y, ret$x^2)

ret <- layer_data(base + scale_x_log10() + scale_y_log10())
expect_equal(nrow(ret), 5)
expect_equal(10^ret$y, (10^ret$x)^2)

# now with explicit mapping of y
base <- ggplot(dat, aes(x, y)) + geom_point() +
stat_function(fun = ~ .x^2, n = 5)

ret <- layer_data(base, 2)
expect_equal(nrow(ret), 5)
expect_equal(ret$y, ret$x^2)

ret <- layer_data(base + scale_x_log10(), 2)
expect_equal(nrow(ret), 5)
expect_equal(ret$y, (10^ret$x)^2)

ret <- layer_data(base + scale_y_log10(), 2)
expect_equal(nrow(ret), 5)
expect_equal(10^ret$y, ret$x^2)

ret <- layer_data(base + scale_x_log10() + scale_y_log10(), 2)
expect_equal(nrow(ret), 5)
expect_equal(10^ret$y, (10^ret$x)^2)
})


test_that("works with formula syntax", {
dat <- data_frame(x = 1:10)

Expand Down