具有多个组的密度图

Joe*_*ing 4 r ggplot2 kernel-density

我想产生类似的东西densityplot()lattice package,采用ggplot2使用多个归集与后mice封装.这是一个可重复的例子:

require(mice)
dt <- nhanes
impute <- mice(dt, seed = 23109)
x11()
densityplot(impute)
Run Code Online (Sandbox Code Playgroud)

哪个产生:

我想在ggplot中改进的densityplot输出

我想对输出有更多的控制(我也将它用作ggplot的学习练习).所以,对于bmi变量,我试过这个:

bar <- NULL
for (i in 1:impute$m) {
    foo <- complete(impute,i)
    foo$imp <- rep(i,nrow(foo))
    foo$col <- rep("#000000",nrow(foo))
    bar <- rbind(bar,foo)
}

imp <-rep(0,nrow(impute$data))
col <- rep("#D55E00", nrow(impute$data))
bar <- rbind(bar,cbind(impute$data,imp,col))
bar$imp <- as.factor(bar$imp)

x11()
ggplot(bar, aes(x=bmi, group=imp, colour=col)) + geom_density()
+ scale_fill_manual(labels=c("Observed", "Imputed"))
Run Code Online (Sandbox Code Playgroud)

产生这个: 在此输入图像描述

所以它有几个问题:

  1. 颜色是错误的.似乎我试图控制颜色是完全错误/被忽略的
  2. 有不需要的水平和垂直线
  3. 我希望图例显示Imputed和Observed但我的代码会给出错误 invalid argument to unary operator

而且,用一行完成的工作似乎做了很多工作densityplot(impute)- 所以我想知道我是否可能完全以错误的方式解决这个问题?

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

0.4.这些图的范围似乎不正确.

ROL*_*OLO 6

使用ggplot2更复杂的原因是你使用densityplot的是鼠标包(mice::densityplot.mids准确地说 - 检查它的代码),而不是格子本身.此函数具有用于绘制内置mids结果类的所有功能mice.如果您尝试相同的使用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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但正如您所看到的,这些图的范围小于densityplot.这种行为应该由参数来控制trimstat_density,但这似乎不工作.修好代码后stat_density得到以下情节:

在此输入图像描述

仍然与densityplot原版不完全相同,但更接近.

编辑:要获得真正的修复,我们需要等待ggplot2的下一个主要版本,请参阅github.


Thi*_*rry 5

您可以要求Hadley为此mids类添加fortify方法.例如

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)
}
Run Code Online (Sandbox Code Playgroud)

在绘图之前,ggplot'强化'非data.frame对象

ggplot(fortify.mids(impute), aes(x = bmi, colour = Imputed, 
   group = Imputation)) +
geom_density() + 
scale_colour_manual(values = c(Imputed = "#000000", Observed = "#D55E00"))
Run Code Online (Sandbox Code Playgroud)

请注意,每个都以'+'结尾.否则命令将完成.这就是传说没有改变的原因.以"+"开头的行导致错误.

在此输入图像描述

您可以将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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述