ggplot2 stat_function with calculated argument for different data subset inside a facet_grid

白昼怎懂夜的黑 提交于 2019-12-03 08:21:16

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 NAs in df.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!