1

I'm drawing eight histograms by means of a for-loop in R and I want to plot them all as one picture (as 2 columns by 4 rows). I was trying to do the same thing as this person did (Multiple histograms in ggplot2), but somehow for me it doesn't work.

This is (part of) my code:

Words = levels(study$Word)
numberOfWords = length(levels(study$Word))

layout(matrix(1:8,4,2,byrow=TRUE))
par(mar=c(2,1,2,1))

for (i in 1:numberOfWords) {
word <- Words[i]
histo <- ggplot(study[study$Word==word,], aes(study$VOT[study$Word==word]))
histo + geom_histogram(aes(y = ..density..), fill = "white", colour = "black", binwidth = ((max(study$VOT[study$Word==word])-min(study$VOT[study$Word==word]))/10)) + labs(x = "VOT (ms)", y = "Density", title = word) + stat_function(fun = dnorm, args = list(mean = mean(study$VOT[study$Word==word], na.rm=T), sd = sd(study$VOT[study$Word==word], na.rm = T)), colour = "blue",  size = 1)
}

I know the for-loop is working correctly, because when I put print() around that last very long line starting with histo + ..., I see it printing my eight histograms in succession, overwriting each other. When I don't write print() (like in the above code), nothing is printed.

My question is simple: how can I stop my histograms from overwriting each other, and have them together in one picture?

Community
  • 1
  • 1
Johanna
  • 1,019
  • 2
  • 9
  • 20

1 Answers1

3

How about using facet_wrap instead of a for loop?

set.seed(1)

study <- mtcars
study$Word <- sample(1:8, nrow(study), replace = TRUE)
study$VOT <- study$mpg
p <- ggplot(study, aes(x = VOT, y = ..density..)) + geom_histogram()
p <- p + facet_wrap(~ Word, nrow = 4, ncol = 2)

enter image description here

Jake Burkhead
  • 6,435
  • 2
  • 21
  • 32
  • Thank you Jake, this is indeed the sort of image that I'm looking to create, but I'm afraid my knowledge of R is (yet) still too limited to see how I could apply the function that you suggest to my own code. I have looked up the documentation for facet_wrap but still don't really understand how it works. Any more suggestions/advice? – Johanna Feb 02 '14 at 23:14
  • It basically splits your data by some variable, Word, in Jake's example, which takes values of `1:8` so that you get 8 plots, each one for 1,2,...,8. – rawr Feb 02 '14 at 23:26
  • @Johanna I changed the example a bit to make it more clear how you would get this with your data. Changing the `binwidth`s for each histogram and adding the `stat_function` part is harder – Jake Burkhead Feb 02 '14 at 23:36
  • Thank you both. When I try your code it doesn't work though. What is the use of `study <- mtcars` and `study$mpg`, as I don't have any variables called `mtcars` and `mpg`? Why do `study` and `study$VOT` need to be redefined anyway? Thanks so much, I appreciate your help. – Johanna Feb 02 '14 at 23:41
  • @Johanna `mtcars` is a sample dataset. Since you didn't provide your data I used one that comes with R. You can ignore anything before `p <- ggplot(...`. Sorry for the confusion. If you want to provide a sample of your dataset you can add the output of `dput(heat(study))` to the question and I'll change the answer to use that – Jake Burkhead Feb 02 '14 at 23:45