title: How to use facet_grid() with geom_histogram()
body: <p>I tried using the <code>facet_grid()</code> for the first time. I plotted histograms with my own data, and the distribution seemed inaccurate when I counted the boxes manually on the graph. I replicated my code using the <code>mtcars</code> data, and the problem seemed to persist.</p>
  
  <p>Here is the histogram produced by ggplot:</p>
  
  <pre><code>dfrm &lt;- mtcars
  dfrm$am &lt;- factor(dfrm$am, levels = c(0,1), labels = c("Automatic", "Manual"))
  dfrm$vs &lt;- factor(dfrm$vs, levels = c(0,1), labels = c("V-engine", "Straight-Engine"))
  
  require(ggplot2)
  ggplot(dfrm, aes(x=dfrm[,"mpg"], fill=dfrm[,"am"], colour=dfrm[,"am"])) +
  geom_histogram(colour="transparent", position = "identity", alpha=0.2, bins = 10) +
  facet_grid(. ~ dfrm[,"vs"])
  </code></pre>
  
  <p>When I count manually on the histogram, I count:</p>
  
  <ul>
  <li>V-Engine, Automatic: 14 </li>
  <li>V-Engine, Manual: 4</li>
  <li>Straight Engine, Automatic: 5</li>
  <li>Straight Engine, Manual: 9</li>
  </ul>
  
  <p>This code counts how many of which exist in the actual data:</p>
  
  <pre><code>require(pastecs)
  by(data=dfrm$am, INDICES = dfrm$vs,  table)
  </code></pre>
  
  <p>and the results are:</p>
  
  <ul>
  <li>V-Engine, Automatic: 12</li>
  <li>V-Engine, Manual: 6</li>
  <li>Straight Engine, Automatic: 7</li>
  <li>Straight Engine, Manual: 7</li>
  </ul>
  
  <p>Am I doing something wrong? Is there a better way to facet, or is this a bug?</p>
  
  <p>I also did histograms with the base package to check if the results match, and those seem accurate when I count the boxes.</p>
  
  <pre><code>hist(mtcars[which(mtcars[,"am"]==0 &amp; mtcars[,"vs"]==0),"mpg"], xlim=c(10, 35), col=rgb(0.1,0.1,0.1,0.5), breaks=10)
  hist(mtcars[which(mtcars[,"am"]==1 &amp; mtcars[,"vs"]==0),"mpg"], col=rgb(0.8,0.8,0.8,0.5), breaks=10 ,add=T)
  hist(mtcars[which(mtcars[,"am"]==0 &amp; mtcars[,"vs"]==1),"mpg"], xlim=c(10, 35), col=rgb(0.1,0.1,0.1,0.5), breaks=10)
  hist(mtcars[which(mtcars[,"am"]==1 &amp; mtcars[,"vs"]==1),"mpg"], col=rgb(0.8,0.8,0.8,0.5), breaks=10 ,add=T)
  </code></pre>
  
  <p>Thanks.</p>
  
  <p>===EDIT===</p>
  
  <p>The answer provided by bdemarest solves the problem. However, I am confused with the syntax that ggplot2 prefers, and how to put it inside a function.
  Here is what I am going for:</p>
  
  <pre><code>myfunc &lt;- function(varx, dfrm, facet = F){
    require(ggplot2)
    p = ggplot(dfrm, aes(x=varx, fill=am)) +
      geom_histogram(position="identity", colour="grey40", alpha=0.2, bins = 10)
    if(!is.logical(facet)){
      p &lt;- p + facet_grid(. ~ facet)
    }
    return(p)
  }
  myfunc("mpg", mtcars, facet = "vs")
  </code></pre>
  
  <p>I tried with and without quotations, but couldn't get it to work.</p>
  
  <p>=== EDIT2 ===</p>
  
  <p>With the help of bdemarest in the comments, I made a lot of progress, but now the color fill fails, but only when the ggplot is inside a function</p>
  
  <p>Here, this works perfectly:</p>
  
  <pre><code>facet = "vs"
  p = ggplot(dfrm, aes_string(x="mpg", fill="am")) +
    geom_histogram(position="identity", colour="grey40", alpha=0.2, bins = 10)
  if(!is.logical(facet)){
    p &lt;- p + facet_grid(reformulate(facet, "."))
  }
  p
  </code></pre>
  
  <p>However, this does not:</p>
  
  <pre><code>myfunc &lt;- function(varx, dfrm, facet = FALSE){
    require(ggplot2)
    p = ggplot(dfrm, aes_string(x=varx, fill="am")) +
      geom_histogram(position="identity", colour="grey40", alpha=0.2, bins = 10)
    if(!is.logical(facet)){
      p &lt;- p + facet_grid(reformulate(facet, "."))
    }
    return(p)
  }
  myfunc("mpg", mtcars, facet = "vs")
  </code></pre>
  
  <p>The only problem here now is that the groups wont get colored accordingly. What am I missing?</p>