Skip to content

stat_function disregards log and possibly other scale #3905

Closed
@arwarw

Description

@arwarw

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions