【问题标题】:Why aren't any points showing up in the qqcomp function when using plotstyle="ggplot"?为什么使用 plotstyle="ggplot" 时 qqcomp 函数中没有显示任何点?
【发布时间】:2020-04-19 02:42:19
【问题描述】:

我想在一个图中比较不同分布与我的数据的拟合度。 fitdistrplus 包中的 qqcomp 函数几乎完全符合我的要求。然而,我唯一的问题是它主要是使用基本 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)

这给出了以下输出:

此时:

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

给出以下输出:

为什么积分不显示?我在这里做错了什么还是这是一个错误?

编辑:

所以我还没有弄清楚为什么这不起作用,但有一个非常简单的解决方法。函数调用qqcomp(model.list, plotstyle="ggplot") 仍然返回一个 ggplot 对象,其中包括用于制作绘图的数据。使用这些数据,人们可以轻松编写自己的绘图函数,完全按照自己的意愿行事。它不是很优雅,但在有人发现它为什么不能按预期工作之前,我会使用这种方法。

【问题讨论】:

    标签: r ggplot2 model-fitting fitdistrplus


    【解决方案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")
    

    希望对你有帮助。

    【讨论】:

    • 我已经编写了自己的绘图函数,但无论如何感谢您的输入 :) 我会联系开发人员。我的猜测是它可能与较新的ggplot2 版本或类似的东西有关。
    • 不客气!我会猜到同样的事情,但我在他们的源代码上没有看到任何错误(也许我只是没有看到)。希望您能得到满意的答复。
    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2019-05-29
    • 1970-01-01
    相关资源
    最近更新 更多