【问题标题】:R using ggplot2 to plot mixEM dataR 使用 ggplot2 绘制 mixEM 数据
【发布时间】:2017-08-21 18:13:01
【问题描述】:

我有一个长度为 370 的向量,我想将其拟合到高斯的混合中。我已经按照这里的示例:Any suggestions for how I can plot mixEM type data using ggplot2 绘制数据,但是从图片链接可以看到,我的结果与示例中的结果不同:Plot of a mixture of three Gaussians

这是我使用的代码的 sn-p:

library(ggplot2)
library(mixtools)

gg.mixEM <- function(EM) {
  require(ggplot2)
  x       <- with(EM,seq(min(x),max(x),len=1000))
  pars    <- with(EM,data.frame(comp=colnames(posterior), mu, sigma,lambda))
  em.df   <- data.frame(x=rep(x,each=nrow(pars)),pars)
  em.df$y <- with(em.df,lambda*dnorm(x,mean=mu,sd=sigma))
  ggplot(data.frame(x=EM$x),aes(x,y=..density..)) + 
    geom_histogram(fill=NA,color="black",bins=41)+
    geom_polygon(data=em.df,aes(x,y,fill=comp),color="grey50", alpha=0.5)+
    scale_fill_discrete("Component\nMeans",labels=format(em.df$mu,digits=3))+
    theme_bw()
}

dput(gradesCS)
c(6.5, 22.375, 20.5, 24.25, 33.25, 24, 26.75, 30.75, 35.5, 23.5, 
26.875, 24, 35.5, 29.875, 29.75, 31.25, 32.875, 33.75, 34, 29, 
33, 24, 12, 26.375, 6.75, 31.25, 21.625, 32.875, 29.25, 27.125, 
28.25, 26.25, 24.875, 35.5, 26.5, 37.5, 35.375, 27.5, 33, 27.5, 
39.5, 34.25, 28.125, 28, 32.625, 37.625, 34.5, 29.5, 38.5, 37.5, 
28.75, 38, 16, 35.75, 30, 33.5, 36, 31.125, 29.75, 32.5, 35, 
24.375, 23.375, 28, 32.125, 36, 31.5, 33.5, 1.5, 30.5, 37, 29.5, 
29.5, 31.125, 32.5, 20.5, 28.75, 30.25, 32.5, 28, 36, 37.5, 28.5, 
35.5, 30.25, 36.375, 36, 23.25, 31.5, 25.125, 33.5, 34, 19.5, 
31.75, 39.5, 33.25, 24.875, 26.75, 23.375, 34, 16.5, 37, 33.375, 
31.25, 31.75, 35.5, 32, 27.5, 23.375, 20.625, 35.5, 31.5, 25.375, 
24.5, 27.25, 25.25, 35.75, 24, 28.25, 33.125, 31.5, 39.5, 39.25, 
24.75, 37, 25.5, 34.75, 34, 20.25, 37.625, 30.5, 32.375, 15, 
32.75, 33.5, 32.75, 31.5, 29.25, 30, 37.25, 34.5, 23, 32.5, 38.25, 
35.625, 33, 35, 31.125, 37, 28.125, 29.25, 31.75, 34.75, 34.625, 
36.625, 15.25, 35.5, 37, 33.5, 30.875, 35, 31.625, 22.75, 31, 
31.125, 25.125, 35.5, 2, 36.125, 25.25, 32.5, 28, 38.5, 35.5, 
38.5, 30.5, 34, 28.125, 38, 29.25, 29.75, 33.25, 25.125, 35, 
34.5, 32, 35, 26.875, 20.5, 35.5, 23.25, 26.25, 36, 35.5, 38, 
39.25, 22, 38.5, 31, 35.5, 33.5, 31.5, 26, 30.375, 35.75, 29.75, 
34, 37.625, 38, 35.5, 34.25, 24.375, 30, 33.75, 39.5, 36.5, 36.5, 
32, 36.5, 29.75, 29.75, 25, 32, 29.25, 32.125, 31.25, 38, 33.5, 
33.5, 38.5, 37.25, 31.125, 33.5, 31, 28, 29.75, 36, 36, 37, 22, 
29, 36.5, 32.25, 30.75, 38.5, 24.125, 28.75, 38.25, 32.5, 34.75, 
29, 30.375, 33.5, 31.25, 30, 33, 33.5, 27.5, 26.5, 30.25, 34.75, 
33.5, 39, 33.25, 38.5, 27, 39.5, 34.25, 33, 35.125, 38, 31.25, 
32.75, 22.75, 31.125, 34.5, 33, 37.125, 31, 18.75, 30.25, 31.75, 
34, 30.75, 29, 34.5, 36, 36.5, 31.5, 26, 27.5, 27.5, 36.5, 19.75, 
33, 35.125, 16, 19.75, 31.5, 38.5, 34.25, 36.5, 27, 22, 21.75, 
36, 31.5, 33, 29.75, 32.5, 26.25, 33.5, 35.75, 33, 39, 35, 34.25, 
28.5, 25.5, 30.5, 28, 21.25, 39.125, 22.75, 28.375, 29.125, 30, 
34.125, 31.25, 32, 26.25, 36, 24.5, 30.25, 32.75, 29.625, 16, 
34, 16.75, 25.25, 33, 38, 28, 24.75, 29.75, 24.5, 19.25, 32.75, 
27.5, 24.75, 17.375, 25.25, 30.125, 38, 28, 35, 11.75, 27.75, 
38, 28.625, 31.25, 31.25, 32, 17.25, 18.25, 32.625, 25.5, 27.5, 
35.25, 35.5)

b <- gradesCS
c <- b[sample(length(b), length(b)) ]
c3 <- normalmixEM(c, lambda=NULL, mu=NULL, sigma=NULL,k=3,maxit=1000,epsilon = 1e-2)
gg.mixEM(c3)

【问题讨论】:

  • 如果您发布带有示例输入数据的reproducible example 会更容易为您提供帮助,以便我们自己运行和测试代码。明确列出您正在使用的任何非基础 R 包。
  • 谢谢。我编辑了问题。

标签: r plot ggplot2


【解决方案1】:

问题是,如果多边形没有连续的绘图空间(例如,如果你突然在 0 处结束,但多边形函数还没有达到 0)。

在 ggplot 函数的第一行,在 x 的每一侧添加额外的间距。我这里是 5,但是你只需要足够的函数来达到 0。

x <- with(EM,seq(min(x)-5,max(x)+5,len=1000))

在底部,我们用

切断了多余的空间
coord_cartesian(xlim = c(0,42),
                expand = c(0,0))

这会以您的间距呈现图表,然后在选定的 x 间隔上“放大”。

fit_test <- normalmixEM(
    test,
    k = 2)

gg.mixEM <- function(EM) {
    require(ggplot2)
    x       <- with(EM,seq(min(x)-5,max(x)+5,len=1000))
    pars    <- with(EM,data.frame(comp=colnames(posterior), mu, sigma,lambda))
    em.df   <- data.frame(x=rep(x,each=nrow(pars)),pars)
    em.df$y <- with(em.df,lambda*dnorm(x,mean=mu,sd=sigma))
    ggplot(data.frame(x=EM$x),aes(x,y=..density..)) + 
        geom_histogram(fill=NA,color="black",bins=41)+
        geom_polygon(data=em.df,aes(x,y,fill=comp),color="grey50", alpha=0.5)+
        scale_fill_discrete("Component\nMeans",labels=format(em.df$mu,digits=3))+
        theme_bw() +
        coord_cartesian(xlim = c(0,42),
                        expand = c(0,0))
}

gg.mixEM(fit_test)

我们得到

【讨论】:

    猜你喜欢
    • 2014-10-08
    • 2018-04-08
    • 2021-02-10
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多