r - Graphing a histogram overlaid with a fitted 2 parameter Weibull function -
i plot both histogram fitted weibull function on same graph. code plot histogram is:
hist(data$grddia2, prob=true,breaks=5)
the code fitted weibull function is:(need mass package)
fitdistr(data$grddia2,densfun=dweibull,start=list(scale=1,shape=2))
how plot both on same graph. i've attached data set.
also, bonus can provide code can achieve same thing, create graph each column of data. many columns within data set. nice have graphs on same page.
https://www.dropbox.com/s/ra9c2kkk49vyyyc/diameter%20distribution.csv?dl=0
here code
library("ggplot2") library("dplyr") library("tidyr") library("mass") # import dataset , filter column "treeno" # use namespace dplyr:: explicitly because of conflict mass:: function "select" data <- read.csv("diameter distribution.csv") %>% dplyr::select(-treeno) # function provide weibull distribution each column # distribution calculated based on estimated scale , shape parameters of input fitweibull <- function(column) { x <- seq(0,7,by=0.01) fitparam <- column %>% unlist %>% fitdistr(densfun=dweibull,start=list(scale=1,shape=2)) return(dweibull(x, scale=fitparam$estimate[1], shape=fitparam$estimate[2])) } # apply function each column consolidate in data.frame fitdata <-data %>% apply(2, as.list) %>% lapply(fun = fitweibull) %>% data.frame() # display graphs multiplyingfactor<-10 ggplot() + geom_histogram(data=gather(data), aes(x=value, group=key, fill=key), alpha=0.2) + geom_line(data=gather(fitdata), aes(x=rep(seq(0,7,by=0.01),ncol(fitdata)), y=multiplyingfactor*value, group=key, color=key))
and output figure
variant: wonderful ggplot2 package can have graphs apart adding final line of code
+ facet_wrap(~ key) + theme(legend.position = "none")
which gives other figure:
Comments
Post a Comment