Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Log Scale on Reverse Cumulative Distribution Plot in ggplot2

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)

works

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

broken

EDIT:

I want the outcome to look like this:

enter image description here

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()

enter image description here

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()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading