3

I want to compare the fit of different distributions to my data in a single plot. The qqcomp function from the fitdistrplus package pretty much does exactly what I want to do. The only problem I have however, is that it's mostly written using base R plot and all my other plots are written in ggplot2. I basically just want to customize the qqcomp plots to look like they have been made in ggplot2.

From the documentation (https://www.rdocumentation.org/packages/fitdistrplus/versions/1.0-14/topics/graphcomp) I get that this is totally possible by setting plotstyle="ggplot". If I do this however, no points are showing up on the plot, even though it worked perfectly without the plotstyle argument. Here is a little example to visualize my problem:

library(fitdistrplus)
library(ggplot2)

set.seed(42)
vec <- rgamma(100, shape=2)

fit.norm <- fitdist(vec, "norm")
fit.gamma <- fitdist(vec, "gamma")
fit.weibull <- fitdist(vec, "weibull")

model.list <- list(fit.norm, fit.gamma, fit.weibull)

qqcomp(model.list)

This gives the following output:

baseR qqplot

While this:

qqcomp(model.list, plotstyle="ggplot")

gives the following output:

ggplot qqplot

Why are the points not showing up? Am I doing something wrong here or is this a bug?

EDIT:

So I haven't figured out why this doesn't work, but there is a pretty easy workaround. The function call qqcomp(model.list, plotstyle="ggplot") still returns an ggplot object, which includes the data used to make the plot. Using that data one can easily write an own plot function that does exactly what one wants. It's not very elegant, but until someone finds out why it's not working as expected I will just use this method.

Denzo
  • 240
  • 1
  • 7

1 Answers1

2

I was able to reproduce your error and indeed, it's really intriguing. Maybe, you should contact developpers of this package to mention this bug.

Otherwise, if you want to reproduce this qqplot using ggplot and stat_qq, passing the corresponding distribution function and the parameters associated (stored in $estimate):

library(ggplot2)
df = data.frame(vec)
ggplot(df, aes(sample = vec))+
  stat_qq(distribution = qgamma, dparams = as.list(fit.gamma$estimate), color = "green")+
  stat_qq(distribution = qnorm, dparams = as.list(fit.norm$estimate), color = "red")+
  stat_qq(distribution = qweibull, dparams = as.list(fit.weibull$estimate), color = "blue")+
  geom_abline(slope = 1, color = "black")+
  labs(title = "Q-Q Plots", x = "Theoritical quantiles", y = "Empirical quantiles")

enter image description here

Hope it will help you.

dc37
  • 15,840
  • 4
  • 15
  • 32
  • I already wrote my own plot function but thank you for your input anyways :) I will contact the developers about it. My guess is that it probably has something to do with a newer `ggplot2` version or something like that. – Denzo Dec 31 '19 at 11:37
  • You're welcome ! I will have guess the same thing but I don't see anything wrong on their source code (maybe I just did not see it). Hopefully, you will get a satisfying reply. – dc37 Dec 31 '19 at 14:35