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

后端 未结 1 1954
孤独总比滥情好
孤独总比滥情好 2021-02-10 00:09

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

相关标签:
1条回答
  • 2021-02-10 00:36

    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.

    0 讨论(0)
提交回复
热议问题