Multiple stat_function on grouped data with ggplot2

前端 未结 2 1196
余生分开走
余生分开走 2021-01-14 09:35

I am studying a data set with multiple observation of a parameter overtime. the data is like:

test<-data.frame(t = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.33, 0         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 10:33

    What about a naive solution, adding iteratively stat_function()?

        cols <- brewer.pal(length(unique(test$t)),"Set1")
        g <- ggplot(data=subset(test, t == 0, select='int'), aes(x=int))
        n <- 1
        for(i in unique(test$t)){ 
            fit <- fitdistr(subset(test, t == i, select='int')$int, "lognormal")
            g <- g+stat_function(fun = dlnorm, 
                                 args=list(mean=fit$estimate[1],sd=fit$estimate[2]), 
                                 col=cols[n])
            n <- n + 1
        }
        g
    

提交回复
热议问题