【问题标题】:Making fitdist plots with ggplot2使用 ggplot2 制作 fitdist 图
【发布时间】:2015-10-17 15:21:19
【问题描述】:

我用fitdistrplus 包中的fitdist 函数拟合了正态分布。使用denscompqqcompcdfcompppcomp,我们可以分别绘制histogram against fitted density functionstheoretical quantiles against empirical onesthe empirical cumulative distribution against fitted distribution functionstheoretical probabilities against empirical ones,如下所示。

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")

我非常有兴趣用ggplot2 制作这些fitdist 情节。 MWE如下:

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)

如何开始使用ggplot2 制作这些fitdist 绘图?

【问题讨论】:

  • 如果这没有附加赏金,我会投票关闭,因为过于广泛。每个图表应该是一个不同的问题(尽管如果您对其中的一个或两个有答案,您可能不需要询问每个图表)。

标签: r plot ggplot2 distribution fitdistrplus


【解决方案1】:

你可以使用类似的东西:

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))

您只需要在运行图形之前定义dfun。在这个例子中,它是一个拉普拉斯分布,但你可以选择任何你想要的,如果你愿意,可以添加更多stat_function

【讨论】:

    猜你喜欢
    • 2021-04-24
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2021-09-23
    • 2013-10-22
    • 1970-01-01
    相关资源
    最近更新 更多