简体   繁体   中英

R ggplot two cumulative distribution functions in the same plot are not correctly displayed

I wanted to graphically illustrate an example of how the continuously ranked probability score is calculated.

For that, I need two (normal) cumulative probability distributions in one plot, the predicted cdf(blue) and the observed cdf(red). The observed cdf is based on one known value, resulting in std=0 . I arbitrarily assigned:

predicted cdf: mean = 10 , std = 0.3

observed cdf: mean = 10 , std = 0

library("ggplot2")
ggplot() +
  theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(size=0.8),
        panel.grid.minor.y = element_line(size=0.8),
        panel.grid.minor.x = element_blank(),
        panel.background = element_blank()) +
  geom_function(fun = pnorm,col="red",size=1, args = list(mean = 10, sd = 0))+
  geom_function(fun = pnorm,col="blue",size=1, args = list(mean = 10, sd = 0.3))+
  scale_x_continuous(limits = c(3, 17))

However, when I plot this, it does not exactly display the outcome that I expect/want:

路口不正确

Normally the intersect should be at 0.5. However, in this case both cdfs meet at 0.4. How can I align this correctly?

Thanks a lot!

Try increasing the resolution of the line

  geom_function(fun = pnorm,col="red",size=1, args = list(mean = 10, sd = 0), n=500)+
  geom_function(fun = pnorm,col="blue",size=1, args = list(mean = 10, sd = 0.3), n=500)+

geom_function works great for nice smooth lines, but when you have a discontinuity like you do when you have sd=0 , then things can get messy. By increasing the number of points drawn, you sample more points near the discontinuity. (The default is n=101 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM