Adding exponential geom_smooth to ggplot2 / R

I'm trying to create sample graphics using ggplot2, and one of the examples I chose was a happy birthday problem , here, using the code โ€œborrowedโ€ from the revolution, the calculated presentation in Oscon.

birthday<-function(n){
    ntests<-1000
    pop<-1:365
    anydup<-function(i){
        any(duplicated(sample(pop,n,replace=TRUE)))
        }
    sum(sapply(seq(ntests), anydup))/ntests
    }

x<-data.frame(x=rep(1:100, each=5)) 
x<-ddply(x, .(x), function(df) {return(data.frame(x=df$x, prob=birthday(df$x)))})
birthdayplot<-ggplot(x, aes(x, prob))+
        geom_point()+geom_smooth()+
        theme_bw()+
        opts(title = "Probability that at least two people share a birthday in a random group")+
        labs(x="Size of Group", y="Probability")

My graph here is what I would call exponential, but geom_smooth is not particularly suitable for data. I tried the loess method, but that didnโ€™t change the situation much. Can anyone suggest how to add a smoother one?

thank

Pavel.

+3
source share
2 answers

, . , , .

birthday<-function(n){
  ntests<-1000
  pop<-1:365
  anydup<-function(i){
    any(duplicated(sample(pop,n,replace=TRUE)))
  }
  data.frame(Dups = sapply(seq(ntests), anydup) * 1, n = n)
}
x<-ddply(x, .(x),function(df) birthday(df$x))

.

ggplot(x, aes(n, Dups)) +
  stat_summary(fun.y = mean, geom = "point") +
  stat_smooth(method = "glm", family = binomial)
+2

x ( , prob 0-1). , , . :

birthdayplot + geom_smooth(span=0.1, colour="red")
+3

Source: https://habr.com/ru/post/1760713/


All Articles