I would like to add the cumulative distribution to one of my ggplots. Somehow, I fail to get the scaling right.
This is the sample:
require(ggplot2)
set.seed(123456)
'dummy df'
df<-data.frame(id=seq(1,100,1),value2= rnorm(100, 1000, 5000))
'set y-axis size'
ylim.prim<-c(0,8)
ylim.sec<-c(0,1.0)
b<-diff(ylim.prim) / diff(ylim.sec)
a<-b*(ylim.prim[1]-ylim.sec[1])
ggplot(df, aes(df$value2))+
geom_histogram()+
stat_ecdf(col="red")+
scale_y_continuous(sec.axis = sec_axis(~(.-a)/b,
breaks = seq(0,1.1,0.1),
name = "Cumulative Distribution"))
Unfortunately, the red line for the cumulative values does not scale correctly.
Can you help? Thanks!
Edit: I'd like the stat_ecdf function to scale up to 1 on the RHS.