I am trying to produce something similar to densityplot()
from the lattice package
, using ggplot2
after using multiple imputation with the mice
package. Here is a reproducible example:
require(mice)
dt <- nhanes
impute <- mice(dt, seed = 23109)
x11()
densityplot(impute)
Which produces:
I would like to have some more control over the output (and I am also using this as a learning exercise for ggplot). So, for the bmi
variable, I tried this:
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"))
which produces this:
So there are several problems with it:
- The colours are wrong. It seems my attempt to control the colours is completely wrong/ignored
- There are unwanted horizontal and vertical lines
- I would like the legend to show Imputed and Observed but my code gives the error
invalid argument to unary operator
Moreover, it seems like quite a lot of work to do what is accomplished in one line with densityplot(impute)
- so I wondered if I might be going about this in the wrong way entirely ?
Edit: I should add the fourth problem, as noted by @ROLO:
.4. The range of the plots seems to be incorrect.