r - Making fitdist plots with ggplot2 -
i fitted normal distribution fitdist function fitdistrplus package. using denscomp, qqcomp, cdfcomp , ppcomp can plot histogram against fitted density functions, theoretical quantiles against empirical ones, the empirical cumulative distribution against fitted distribution functions, , theoretical probabilities against empirical ones respectively given below.
set.seed(12345) df <- rnorm(n=10, mean = 0, sd =1) library(fitdistrplus) fm1 <-fitdist(data = df, distr = "norm") summary(fm1) denscomp(ft = fm1, legendtext = "normal") qqcomp(ft = fm1, legendtext = "normal") cdfcomp(ft = fm1, legendtext = "normal") ppcomp(ft = fm1, legendtext = "normal") i'm keenly interested make these fitdist plots ggplot2. mwe below:
qplot(df, geom = 'blank') + geom_line(aes(y = ..density.., colour = 'empirical'), stat = 'density') + geom_histogram(aes(y = ..density..), fill = 'gray90', colour = 'gray40') + geom_line(stat = 'function', fun = dnorm, args = as.list(fm1$estimate), aes(colour = 'normal')) + scale_colour_manual(name = 'density', values = c('red', 'blue')) ggplot(data=df, aes(sample = df)) + stat_qq(dist = "norm", dparam = fm1$estimate) i'd highly appreciate if give me hints make these fitdist plots ggplot2. thanks
you use that:
library(ggplot2) ggplot(dataset, aes(x=variable)) + geom_histogram(aes(y=..density..),binwidth=.5, colour="black", fill="white") + stat_function(fun=dnorm, args=list(mean=mean(z), sd=sd(z)), aes(colour = "gaussian", linetype = "gaussian")) + stat_function(fun=dfun, aes(colour = "laplace", linetype = "laplace")) + scale_colour_manual('',values=c("gaussian"="red", "laplace"="blue"))+ scale_linetype_manual('',values=c("gaussian"=1,"laplace"=1)) you need define dfun before running graphic. in example, it's laplace distribution can pick want , add more stat_function if want.





Comments
Post a Comment