使用plotstyle =“ ggplot”时,为什么在qqcomp函数中没有显示任何点?

时间:2019-12-30 10:34:49

标签: r ggplot2 model-fitting fitdistrplus

我想在一张图中比较不同分布与数据的拟合度。 qqcomp包中的fitdistrplus函数几乎完全可以实现我想要的功能。但是,我唯一的问题是,它大多是使用基R图编写的,而我所有其他图都是用ggplot2编写的。我基本上只是想自定义qqcomp图,看起来像是在ggplot2中制作的。

从文档(https://www.rdocumentation.org/packages/fitdistrplus/versions/1.0-14/topics/graphcomp)中我可以知道,完全可以通过设置plotstyle="ggplot"来实现。但是,如果我这样做,即使没有plotstyle参数也可以很好地工作,图中也没有点。这是一个可视化我的问题的小示例:

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)

这将提供以下输出:

baseR qqplot

与此同时:

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

给出以下输出:

ggplot qqplot

为什么这些点没有显示出来?我在这里做错了还是这是一个错误?

编辑:

所以我还没有弄清楚为什么这不起作用,但是有一个非常简单的解决方法。函数调用qqcomp(model.list, plotstyle="ggplot")仍返回ggplot对象,该对象包含用于绘制的数据。使用这些数据,可以轻松编写自己的绘图函数,该函数可以完全满足您的需求。它不是很优雅,但是直到有人发现为什么它无法按预期工作时,我才使用这种方法。

1 个答案:

答案 0 :(得分:1)

我能够重现您的错误,的确,这确实很有趣。也许,您应该联系此软件包的开发人员以提及此错误。

否则,如果要使用ggplotstat_qq重现此qqplot,请传递相应的分布函数和相关的参数(存储在$ 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

希望它会对您有所帮助。