How can I make ggplot2 use a logarithmic scale on the y axis for a reverse CDF?
This works:
rnorm(64) %>%
tibble(kbps = .) %>%
ggplot() +
stat_ecdf(aes(x = kbps, y = 1 - ..y..), pad=FALSE)
But this does not:
rnorm(64) %>%
tibble(kbps = .) %>%
ggplot() +
stat_ecdf(aes(x = kbps, y = 1 - ..y..), pad=FALSE) +
scale_y_log10()
Warning: NaNs produced
Warning: [38;5;232mTransformation introduced infinite values in continuous y-axis[39m
Warning: [38;5;232mRemoved 64 rows containing missing values (`geom_step()`).[39m
EDIT:
I want the outcome to look like this:
Image taken from: https://cacm.acm.org/magazines/2018/7/229031-always-measure-one-level-deeper/fulltext
>Solution :
There’s a couple of ways to do this. You could simply log manually and inverse transform the labels in scale_y_continuous
rnorm(64) %>%
tibble(kbps = .) %>%
ggplot() +
stat_ecdf(aes(x = kbps, y = log10(1 - after_stat(y))), pad = FALSE) +
scale_y_continuous('1 - cdf (log scale)', breaks = seq(-3, 0),
labels = 10^(seq(-3, 0)),
limits = c(-3, 0)) +
theme_bw()
Or precalculate 1 – ecdf and use scale_y_log10 with geom_step
tibble(kbps = rnorm(1000)) |>
arrange(kbps) |>
mutate(ecdf = 1 - (row_number() - 1) / n()) %>%
ggplot(aes(kbps, ecdf)) +
geom_step() +
scale_y_log10() +
theme_bw()




