I have a follow up question to how to pass fitdistr
calculated args to stat_function
(see here for context).
My data frame is like that (see b
This is not possible using stat_function(...)
- see this link, especially Hadley Wickham's comments.
You have to do it the hard way, which is to say, calculating the function values external to ggplot
. Fortunately, this is not all that difficult.
library(MASS)
library(ggplot2)
df <- aggregate(int~Exp+t,small_data,
function(z)with(fitdistr(z,"lognormal"),c(estimate[1],estimate[2])))
df <- data.frame(df[,1:2],df[,3])
x <- with(small_data,seq(min(int),max(int),len=100))
gg <- data.frame(x=rep(x,each=nrow(df)),df)
gg$y <- with(gg,dlnorm(x,meanlog,sdlog))
ggplot(small_data,(aes(x=int)))+
geom_histogram(aes(x=int,y = ..density..),binwidth =150,
color="grey50",fill="lightgreen")+
geom_line(data=gg, aes(x,y,color=t))+
facet_grid(Exp~t)+
scale_colour_gradient2(low='red',mid='blue',high='green',midpoint=5)
So this code creates a data frame df
containing meanlog
and sdlog
for every combination of Exp
and t
. Then we create an "auxillary data frame", gg
, which has a set of x-values covering your range in int
with 100 steps, and replicate that for every combination of Exp
and t
, and we add a column of y-values using dlnorm(x,meanlog,sdlog)
. Then we add a geom_line layer to the plot using gg
as the dataset.
Note that fitdistr(...)
does not always converge, so you should check for NA
s in df
.