The distribution function is not set by magic. You must do this explicitly. One way is to use fitdistr(...) in the MASS package.
library(MASS) # for fitsidtr(...) # excellent fit (of course...) ggplot(df, aes(x = x)) + geom_histogram(aes(y=..density..),colour = "black", fill = "white", binwidth = 0.01)+ stat_function(fun=dbeta,args=fitdistr(df$x,"beta",start=list(shape1=1,shape2=1))$estimate)

# horrible fit - no surprise here ggplot(df, aes(x = x)) + geom_histogram(aes(y=..density..),colour = "black", fill = "white", binwidth = 0.01)+ stat_function(fun=dnorm,args=fitdistr(df$x,"normal")$estimate)

# mediocre fit - also not surprising... ggplot(df, aes(x = x)) + geom_histogram(aes(y=..density..),colour = "black", fill = "white", binwidth = 0.01)+ stat_function(fun=dgamma,args=fitdistr(df$x,"gamma")$estimate)

EDIT : response to OP comment.
The scale factor is binwidth ✕ sample size.
ggplot(df, aes(x = x)) + geom_histogram(colour = "black", fill = "white", binwidth = 0.01)+ stat_function(fun=function(x,shape1,shape2)0.01*nrow(df)*dbeta(x,shape1,shape2), args=fitdistr(df$x,"beta",start=list(shape1=1,shape2=1))$estimate)
