Skip to content

Faulty y-axis for plots based on stat_ecdf with scale_y_log10 #5113

Closed

Description

@m-pilarski

When I'm log-scaling the y-axis of CDF-plots (stat_ecdf), the y-values are drawn between 1 and 10 instead of 0 and 1. The y-values of the data extracted from the plot-object (ggplot_build(...)$data[[1]]$y) are ranging from 0 to 1 instead of -1 to 0. When I'm log-scaling the x-axis of CDF-Plots or the x- or y-axis of other plots (e.g. geom_line), everything is drawn as expected.

library(ggplot2)

df <- data.frame(var=1:10/10)

plot_ecdf_ident <- ggplot(df, aes(x=var)) + stat_ecdf()
plot_ecdf_log10_y <- plot_ecdf_ident + scale_y_log10()
plot_ecdf_log10_x <- plot_ecdf_ident + scale_x_log10()

plot_line_ident <- ggplot(df, aes(x=var, y=var)) + geom_line()
plot_line_log10_y <- plot_line_ident + scale_y_log10()
plot_line_log10_x <- plot_line_ident + scale_x_log10()

patchwork::wrap_plots(
  plot_ecdf_ident, plot_ecdf_log10_y, plot_ecdf_log10_x, 
  plot_line_ident, plot_line_log10_y, plot_line_log10_x, 
  nrow=2
)

range(ggplot_build(plot_ecdf_log10_y)$data[[1]]$y)
#> [1] 0 1
range(ggplot_build(plot_line_log10_y)$data[[1]]$y)
#> [1] -1  0

packageVersion("ggplot2")
#> [1] '3.4.0'
packageVersion("scales")
#> [1] '1.2.1'

Created on 2022-12-20 with reprex v2.0.2

Activity

  1. m-pilarski commented on Dec 21, 2022

    @m-pilarski
    Author

    I found out that the faulty log-scaling has nothing to do with the default pad=TRUE in stat_ecdf. I also found out that the transformation woks, when I'm feeding the the data extracted from the untransformed plot to a new plot with geom_step.

    library(ggplot2)
    
    `%>%` <- magrittr::`%>%`
    
    df <- data.frame(var=1:10/10)
    
    plot_ecdf_ident_nopad_1 <- ggplot(df, aes(x=var)) + stat_ecdf(pad=FALSE)
    
    plot_ecdf_log10_y_nopad_1 <- plot_ecdf_ident_nopad_1 + scale_y_log10()
    
    plot_ecdf_ident_nopad_2 <- 
      ggplot_build(plot_ecdf_ident_nopad_1) %>% 
      purrr::chuck("data", 1) %>% 
      ggplot(aes(x=x, y=y, group=group)) +
      geom_step()
    
    plot_ecdf_log10_y_nopad_2 <- plot_ecdf_ident_nopad_2 + scale_y_log10()
    
    patchwork::wrap_plots(
      plot_ecdf_ident_nopad_1, plot_ecdf_log10_y_nopad_1,
      plot_ecdf_ident_nopad_2, plot_ecdf_log10_y_nopad_2,
      nrow=2
    )

    Created on 2022-12-21 with reprex v2.0.2

  2. added
    bug
    on Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions