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

stat_function disregards log and possibly other scale #3905

Closed
arwarw opened this issue Mar 21, 2020 · 3 comments · Fixed by #3908
Closed

stat_function disregards log and possibly other scale #3905

arwarw opened this issue Mar 21, 2020 · 3 comments · Fixed by #3908
Labels
bug an unexpected problem or unintended behavior

Comments

@arwarw
Copy link

arwarw commented Mar 21, 2020

stat_function() seems to disregard scale_y_log10() and similar. There has been a PR #1011 that is (as far as I understand) supposed to have fixed this issue, merged in 2015 and released in 1.0.1.9000.

However, I can observe the following behavior in ggplot2 3.2.1 and 3.3.0 (did not test others):

library(tidyverse)
fn <- function(x) {exp(x)}
data <- tibble(x = c(1:10), y=exp(x))
data %>% ggplot(aes(x, y)) + geom_point() + stat_function(fun=fn)

This (above) looks as expected, nothing was rescaled, the function matches the data points.

data %>% ggplot(aes(x, y)) + geom_point() + stat_function(fun=fn) + scale_y_log10()

The problem can be observed here, while the data points were adapted to a logscale y axis, the function wasn't.

data %>% ggplot(aes(x, y)) + geom_point() + stat_function(fun=function(x){log10(fn(x))}) + scale_y_log10()

This is what I would the plot to look like (just without the workaround of applying the extra log10()).

Created on 2020-03-21 by the reprex package (v0.3.0)

@clauswilke
Copy link
Member

I vaguely recall that this also has bothered me in the past. Let's see if we can figure out a way to fix this.

@clauswilke
Copy link
Member

Looking at the code, it seems to me that transformations along the x axis are handled correctly but transformations along y are not:

ggplot2/R/stat-function.r

Lines 109 to 111 in da2388e

# For continuous scales, need to back transform from transformed range
# to original values
x_trans <- scales$x$trans$inverse(xseq)

Reprex:

library(tidyverse)
fn <- function(x) {exp(x)}
data <- tibble(x = c(1:10), y=exp(x))
data %>% ggplot(aes(x, y)) + geom_point() + stat_function(fun=fn)

data %>% ggplot(aes(x, y)) + geom_point() + stat_function(fun=fn) +
  scale_x_log10()

Created on 2020-03-21 by the reprex package (v0.3.0)

@clauswilke
Copy link
Member

I've prepared a PR that fixes this. #3908

@clauswilke clauswilke added this to the ggplot2 3.3.1 milestone Mar 22, 2020
@thomasp85 thomasp85 added the bug an unexpected problem or unintended behavior label Apr 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants