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

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

Closed
m-pilarski opened this issue Dec 20, 2022 · 1 comment
Closed

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

m-pilarski opened this issue Dec 20, 2022 · 1 comment
Labels
bug an unexpected problem or unintended behavior

Comments

@m-pilarski
Copy link

m-pilarski commented Dec 20, 2022

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

@m-pilarski
Copy link
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

@teunbrand teunbrand added the bug an unexpected problem or unintended behavior label Dec 23, 2022
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

No branches or pull requests

2 participants