title: ggplot 2 use facet wrap with multiple stacked barplots of frequency counts
body: <p>I have a number of binary variables where I want to express the frequency of behaviours occurring (1 = present, 0 = absent) according to condition. I can successfully plot this for individual variables using the syntax below:</p>
  <pre><code>require(tidyverse)
  require(ggplot2)
  require(ggsignif)
  require(ggpubr)
  
  
  condition &lt;- c(&quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;)
  binary_1 &lt;- c(0,0,0,0,0,1,1,1,1,1)
  binary_2 &lt;- c(1,1,1,1,1,1,0,0,0,0)
  binary_3 &lt;- c(0,1,1,1,1,1,1,1,0,0)
  binary_4 &lt;- c(1,1,1,0,0,0,0,0,0,0)
  
  
  df &lt;- data.frame(condition, binary_1, binary_2, binary_3, binary_4)
  df
  
  gg_df &lt;- df %&gt;%
    mutate(binary_1 = as.factor(binary_1), binary_2 = as.factor(binary_2), binary_3 = as.factor(binary_3), binary_4 = as.factor(binary_4))
  
  gg_melt &lt;- melt(gg_df)
  
  # example for one of the variables (binary_1), I just swap the variable out for each graph
  gg_1 &lt;- ggplot(gg_melt, aes(x=condition, fill = binary_1)) +
    geom_bar(stat=&quot;count&quot;) +
    scale_fill_manual(values = c(&quot;#FDAE61&quot;, &quot;#9E0142&quot;), name = &quot;Behaviour Observed&quot;, labels = c(&quot;0&quot; = &quot;Absent&quot;, &quot;1&quot; = &quot;Present&quot;)) +
    scale_x_discrete(labels = c(a = &quot;Condition A&quot;, b = &quot;Condition B&quot;, c = &quot;Condition C&quot;)) + 
    xlab(&quot;Condition&quot;) + 
    ylab(&quot;Number of Participants&quot;) +
    theme(aspect.ratio = 1)
  </code></pre>
  <p>However, since all of the variables have the same x-axis (condition) and y-axis (frequency count) I was hoping to use facet-wrap to express all the variables simultaneously on the same graph. However, I haven't seemed to be able to get it to work. I read through the following threads:</p>
  <p><a href="https://stackoverflow.com/questions/47544461/how-to-pass-a-count-of-observations-to-the-facet-label">How to pass a count of observations to the facet label?</a></p>
  <p><a href="https://stackoverflow.com/questions/38562040/r-ggplot2-using-count-when-using-facet-grid">R ggplot2 using ..count.. when using facet_grid</a></p>
  <p><a href="https://stackoverflow.com/questions/4725339/percentage-on-y-lab-in-a-faceted-ggplot-barchart">percentage on y lab in a faceted ggplot barchart?</a></p>
  <p>But I can't work out how to split by the variables, and not the count data.</p>
  <pre><code># Attempt 1
  gg_df %&gt;% 
    group_by(condition) %&gt;%
    mutate(beh_count = n()) %&gt;%
    ungroup() %&gt;%
    mutate(beh_updated = paste0(condition, &quot;; n=&quot;, beh_count)) %&gt;%
    ggplot(aes(x = condition)) + geom_bar()+
    facet_wrap(~beh_updated)
  
  # Attempt 2
  gg_df %&gt;% 
    ggplot(aes(x = condition)) + geom_bar()+
    facet_wrap(~binary_1 + binary_2 + binary_3 + binary_4)
  
  # Attempt 3
  ggplot(data = gg_df) + 
    geom_bar(aes(condition)) + 
    facet_wrap(~binary_1 + binary_2 + binary_3 + binary_4)
  </code></pre>
  <p>How can I create a data frame with both the count data and the variables to be able to use facet wrap correctly?</p>