R 多组密度图

R 多组密度图,r,ggplot2,kernel-density,R,Ggplot2,Kernel Density,我试图从晶格包中产生类似于densityplot()的东西,在对小鼠包使用多重插补后,使用ggplot2。以下是一个可复制的示例: require(mice) dt <- nhanes impute <- mice(dt, seed = 23109) x11() densityplot(impute) 这就产生了: 因此,它有几个问题: 颜色不对。看来我试图控制颜色是完全错误的/被忽视了 有不需要的水平线和垂直线 我希望图例显示插补和观察,但我的代码将错误无效参数提供给一元运算符

我试图从
晶格包
中产生类似于
densityplot()
的东西,在对
小鼠
包使用多重插补后,使用
ggplot2
。以下是一个可复制的示例:

require(mice)
dt <- nhanes
impute <- mice(dt, seed = 23109)
x11()
densityplot(impute)
这就产生了:

因此,它有几个问题:

  • 颜色不对。看来我试图控制颜色是完全错误的/被忽视了
  • 有不需要的水平线和垂直线
  • 我希望图例显示插补和观察,但我的代码将错误
    无效参数提供给一元运算符
  • 此外,使用
    densityplot(impute)
    在一行中完成的工作似乎相当多,所以我想知道我是否完全以错误的方式完成了这项工作

    编辑:我应该添加第四个问题,如@ROLO所述:


    .4。绘图范围似乎不正确。

    您可以要求Hadley为此mids类添加一个强化方法。例如

    fortify.mids <- function(x){
     imps <- do.call(rbind, lapply(seq_len(x$m), function(i){
       data.frame(complete(x, i), Imputation = i, Imputed = "Imputed")
     }))
     orig <- cbind(x$data, Imputation = NA, Imputed = "Observed")
     rbind(imps, orig)
    }
    
    请注意,每个都以“+”结尾。否则,该命令将完成。这就是为什么传说没有改变。以“+”开头的行导致了错误

    您可以融合fortify.mids的结果,在一个图形中绘制所有变量

    library(reshape)
    Molten <- melt(fortify.mids(impute), id.vars = c("Imputation", "Imputed"))
    ggplot(Molten, aes(x = value, colour = Imputed, group = Imputation)) + 
    geom_density() + 
    scale_colour_manual(values = c(Imputed = "#000000", Observed = "#D55E00")) +
    facet_wrap(~variable, scales = "free")
    
    库(重塑)
    
    使用ggplot2更复杂的原因是您使用的是mice软件包(
    mice::densityplot.mids
    )中的
    densityplot
    ,准确地说,是检查其代码),而不是晶格本身。此函数具有内置的从
    鼠标绘制
    mids
    结果类的所有功能。如果您使用
    lattice::densityplot
    尝试同样的方法,您会发现它至少与使用ggplot2一样多

    但不用多说,下面是如何使用ggplot2:

    require(reshape2)
    # Obtain the imputed data, together with the original data
    imp <- complete(impute,"long", include=TRUE)
    # Melt into long format
    imp <- melt(imp, c(".imp",".id","age"))
    # Add a variable for the plot legend
    imp$Imputed<-ifelse(imp$".imp"==0,"Observed","Imputed")
    
    # Plot. Be sure to use stat_density instead of geom_density in order
    #  to prevent what you call "unwanted horizontal and vertical lines"
    ggplot(imp, aes(x=value, group=.imp, colour=Imputed)) + 
        stat_density(geom = "path",position = "identity") +
        facet_wrap(~variable, ncol=2, scales="free")
    
    require(重塑2)
    #获取插补数据和原始数据
    
    这真是太好了。谢谢你,(+1)。请问,是否可以防止水平线和垂直线打印?:geom_density()创建多边形,而不是直线。因此,垂直线和水平线。多边形的优点是,可以使用轮廓颜色和填充颜色。再见。谢谢你(+1)。请问您是如何修改代码来扩展范围的?当然可以。看见只是一个快速的攻击。我仍在研究软件包开发,当这一点可行并且我有时间时,我将寻找更好的解决方案,并将其提交给
    ggplot2
    devs。再次感谢。这很有效。我很抱歉再问一个问题,但我是R的初学者-我理解你对代码所做的更改,但是你能告诉我你如何找到你必须更改的代码吗?我查了一下。另请参阅这篇文章,主题是比较不同组中R的密度:
    library(reshape)
    Molten <- melt(fortify.mids(impute), id.vars = c("Imputation", "Imputed"))
    ggplot(Molten, aes(x = value, colour = Imputed, group = Imputation)) + 
    geom_density() + 
    scale_colour_manual(values = c(Imputed = "#000000", Observed = "#D55E00")) +
    facet_wrap(~variable, scales = "free")
    
    require(reshape2)
    # Obtain the imputed data, together with the original data
    imp <- complete(impute,"long", include=TRUE)
    # Melt into long format
    imp <- melt(imp, c(".imp",".id","age"))
    # Add a variable for the plot legend
    imp$Imputed<-ifelse(imp$".imp"==0,"Observed","Imputed")
    
    # Plot. Be sure to use stat_density instead of geom_density in order
    #  to prevent what you call "unwanted horizontal and vertical lines"
    ggplot(imp, aes(x=value, group=.imp, colour=Imputed)) + 
        stat_density(geom = "path",position = "identity") +
        facet_wrap(~variable, ncol=2, scales="free")