0

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.

Juan
  • 171
  • 1
  • 12
  • How are you expecting the secondary axis to scale? Are you expecting a 1 on the secondary axis to align with an 8 on the primary axis? – ulfelder Jan 12 '20 at 12:43
  • see edit. So, yes I'd like the actual graph and not only the scaling go all the way up to 1 – Juan Jan 12 '20 at 12:51
  • 1
    See [this question](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales) for some discussion of this. From the answers, I gather it's not possible to do what you want with `ggplot2`. – ulfelder Jan 12 '20 at 15:54
  • Thanks for the link. – Juan Jan 13 '20 at 12:34

0 Answers0