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.
source
share