Search code examples
rplotnormal-distributionfitdistrplus

Plot multiple fitdist objects in same the plot with different colors?


I have a list of fitdist objects, which I have stored using this piece of code.

norm_dist_res <- list()
for(i in 1:10)
{
  x <- 1+(8000*(i-1))
  y <- 8000*i
  print (x)
  print(y)
  norm_dist_res[[i]] = norm_dist_res[[i]] <- fitdist(data=as.vector(g_all_p$data[x:y,]), distr="norm")

}

Is there a way to plot all the normal distributions extracted from fittest with a different color to show how the data is distributed?

Or in general how to visualize multiple normal distributions?


Solution

  • You are estimating the parameters of a normal distribution, so just plot the densities.

    ## Don't no what g_all_p is, so simplifying the data
    library(fitdistrplus)
    norm_dist_res <- list()
    for(i in 1:10)
    {
      norm_dist_res[[i]] = norm_dist_res[[i]] <- fitdist(data=rnorm(10), distr="norm")
    
    }
    

    Then just plot using lines and changing the colour

    x = seq(-5, 5, length.out=100)
    plot(x, type="n", ylim=c(0, 1), xlim=range(x))
    for(i in 1:10) {
      est = norm_dist_res[[i]]$estimate
      lines(x, dnorm(x, est[1], est[2]), col="grey90")
    }
    

    To get

    enter image description here