0

I want to print the output from densityplot() from the mice package (densityplot method for mids objects), each to a separate page on a PDF. For the MWE below, I want to print one plot on each page (i.e., bmi on page 1, then hyp on page 2, then chi on page 3).

library(mice)

imp <- mice(nhanes)

densityplot(imp)

densityplots

EDIT:

My slightly modified solution (to do this programatically):

# Which variables had missing observations
imputed_vars <- names(d_imp$method)[d_imp$method != ""]

pdf(file = "./MICE Density Plots.pdf")

# Plot each imputed variable separately
for(var in imputed_vars){
  densityplot(d_imp, formula(paste("~", var)))
}

dev.off()
Brigadeiro
  • 2,649
  • 13
  • 30
  • open a new graphics device for each plot? – Nate Mar 05 '21 at 17:21
  • How would I do that? I just call `densityplot()` once to produce all the plots – Brigadeiro Mar 05 '21 at 17:25
  • If you want document like that I'd use Rmd and the `\newpage` special: https://stackoverflow.com/questions/25240541/how-to-add-newpage-in-rmarkdown-in-a-smart-way. This is how you use a PDF graphics device: https://stackoverflow.com/questions/20016215/r-plot-and-save-in-a-pdf-file – Nate Mar 05 '21 at 19:22
  • My question isn't about how to save output to a PDF, but how to specifically save the plots returned from `densityplot.mids()` to separate PDF pages. Run the MWE and you will see what I'm talking about. – Brigadeiro Mar 07 '21 at 00:41

1 Answers1

2

you mean something like this:

imp <- mice(nhanes)
densityplot(imp)
densityplot(imp, ~bmi)
densityplot(imp, ~hyp)
densityplot(imp, ~chl)

BR