Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

r - ggplot stat_ecdf with coord_trans and custom ticks

I would like to make a custom CDF where I can view the top part of the CDF with even spacing between 0.9, 0.99, 0.999, etc.

To do this, I tried the following code:

library(ggplot2)
library(scales)

log_tail_trans = function() trans_new("log_tail", function(x) -log10(1-x), function(y) 1 - 10^(-y), breaks=c(0.9, 0.99, 0.999))

p <- ggplot(df, aes(value)) +
  stat_ecdf(geom='step') +  
  coord_trans(y = 'log_tail') +
  theme_bw() +
  ylim(c(0.68, 0.999999)) +
  xlim(c(0, 1000))

This appears to produce the CDF I want, but even though I've specified breaks, I get the default breaks, as seen below:

CDF with default breaks

There's a previous SO question where someone has the same issue without a CDF and the accepted answer is to use scale_y_continuous to modify the breaks after applying coord_trans, but if do the same thing with a CDF, there's no change and I get the plot above.

p <- ggplot(df, aes(value)) +
  stat_ecdf(geom='step') +  
  coord_trans(y = 'log_tail') +
  scale_y_continuous(breaks=c(0.9, 0.99, 0.999), labels=c(0.9, 0.99, 0.999)) +
  theme_bw() +
  ylim(c(0.68, 0.999999)) +
  xlim(c(0, 1000))

I've also tried passing my transformation into scale_y_continuous, but if I do that with a CDF, then no transformation occurs and I still get the default breaks even though I'm specifying breaks and labels.

enter image description here

One way I could solve this problem is to "manually" compute the CDF and do the transform myself and then set custom labels, but is there something I'm doing wrong in the above that I need to change or is there an alternate method I can use that takes advantage of ggplot (or tidyverse or other pre-existing) functionality?

This is with R 4.0.3, ggplot 3.3.3, and scales 1.1.1.

question from:https://stackoverflow.com/questions/65951284/ggplot-stat-ecdf-with-coord-trans-and-custom-ticks

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...