Question

Despite using ggplot a few times already, I am really struggling to make any headway with this problem. I guess it will be easiest to explain what I want to do without my abominable attempt at using ggplot, The following is quite ugly too, but it's the best I can manage at the moment :(

require(mice)
impute <- mice(nhanes, seed = 101)

counts <- table(nhanes$hyp)
barplot(counts, main="hyp", xlab="observed")

x11()
counts <- table(complete(impute,1)$hyp)
barplot(counts, main="hyp", xlab="Imputation 1")

x11()
counts <- table(complete(impute,2)$hyp)
barplot(counts, main="hyp", xlab="Imputation 2")

x11()
counts <- table(complete(impute,3)$hyp)
barplot(counts, main="hyp", xlab="Imputation 3")

x11()
counts <- table(complete(impute,4)$hyp)
barplot(counts, main="hyp", xlab="Imputation 4")

x11()
counts <- table(complete(impute,5)$hyp)
barplot(counts, main="hyp", xlab="Imputation 5")

I would like to create a nice looking grid of plots showing these kind of barplots in ggplot - for example, 1 row of 6, all with the same scale on the y axis so that they can be easily compared.

I think I should use ldt <-complete(impute,"long", include=TRUE) and then melt(ldt, c(".imp",".id","hyp")) but I just can't work out how to call ggplot after that :(

Please note that I have many different variables in my real data, and this only applies to the categorical variables. I was thinking I could make make a function and then run that using sapply, but only on the categorical columns ? But I don't have much idea of how to do that !

Was it helpful?

Solution

A couple of points

  1. You need to have hyp or the variable in question as a factor. It is easiest to remove the NA values before you do this.
  2. facet_wrap (one variable) or facet_grid (one or more variables) will arrange the plots nicely for you.

For example

ldt <-complete(impute,"long", include=TRUE)

ggplot(ldt[!is.na(ldt$hyp),], aes(x= factor(hyp))) + 
  geom_bar() + 
  facet_wrap(~.imp, nrow = 1) +
  xlab('Observed') +
  scale_y_continuous(expand = c(0,0))

enter image description here

in long form

Now you want facet_grid with scales = 'free_y'

all_long <- melt(ldt, c(".imp",".id","hyp"))
ggplot(all_long[!is.na(all_long$hyp),], aes(x= factor(hyp))) + 
  geom_bar() + 
  facet_grid(variable ~.imp, scales = 'free_y') +
  xlab('Observed') +
  scale_y_continuous(expand = c(0,0))

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top