title: Stacked Barplot with Frequency Counts ggplot2
body: <p>I have a dataset where I have several conditions, and I want to create a stacked bar graph showing the frequency of errors occurring in each condition. (so the number of cases in each condition where 1 error occurred, 2 errors occurred 3 errors occurred... etc etc.)</p>
  <p>In theory, I understand the principle of creating bar graphs with ggplot2. However, the problem I am having is that the 'frequency' count is not an actual variable in the data frame (as it requires counting the number of cases). I'm not sure how to add it in to the gpplot2 framework (potentially using the 'stat' function, but I'm not so certain how this works).</p>
  <p>I checked out the following similar questions:</p>
  <p><a href="https://stackoverflow.com/questions/3548199/how-to-barplot-frequencies-with-ggplot2">How to barplot frequencies with ggplot2?</a></p>
  <p><a href="https://stackoverflow.com/questions/45603370/r-stacked-frequency-histogram-with-percentage-of-aggregated-data-based-on">R stacked % frequency histogram with percentage of aggregated data based on</a></p>
  <p><a href="https://stackoverflow.com/questions/19835987/display-frequency-instead-of-count-with-geom-bar-in-ggplot">Display frequency instead of count with geom_bar() in ggplot</a></p>
  <p><a href="https://stackoverflow.com/questions/47418943/how-to-label-stacked-histogram-in-ggplot">How to label stacked histogram in ggplot</a></p>
  <p>But none of them really provide the answer I'm looking for (i.e., how to count the number of cases for each 'error' and include that into the ggplot2 code.</p>
  <p>Below are some of my attempts with example data</p>
  <pre><code>library(tidyverse)
  
  condition &lt;- c(&quot;condition 1&quot;, &quot;condition 2&quot;, &quot;condition 3&quot;, &quot;condition 1&quot;, &quot;condition 2&quot;, &quot;condition 3&quot;, &quot;condition 1&quot;, &quot;condition 2&quot;, &quot;condition 3&quot;, &quot;condition 1&quot;, &quot;condition 2&quot;, &quot;condition 3&quot;, &quot;condition 1&quot;, &quot;condition 2&quot;, &quot;condition 3&quot;)
  number_of_errors &lt;- c(1,2,3,3,2,1,4,4,5,4,5,1,2,2,3)
  
  df &lt;- data.frame(condition, number_of_errors)
  df
  
  df_melt &lt;-melt(df) #This creates a data frame with 3 columns, 'condition', 'variable' and 'value' where 'variable' just says 'number_of_errors' for each row
  
  
  # Attempt 1 - (Error: stat_bin() can only have an x or y aesthetic.)
  ggplot(df_melt, aes(x=condition, y = variable, fill=value)) + 
    geom_bar(stat=&quot;bin&quot;, position=&quot;stack&quot;) +
    xlab(&quot;Condition&quot;) + 
    ylab(&quot;Frequency of Errors&quot;)
  
  
  # Attempt 2 (produces a graph, but not a stacked one, just the total number of cases in each condition)
  ggplot(df_melt, aes(x = condition, fill = value, label = value)) +
    geom_bar(col=&quot;black&quot;) +
    stat_count(position=&quot;stack&quot;)
  
  
  # Attempt 3 (also produces a graph, but again not a stacked one - I think it is the sum of the number of errors?)
  ggplot(df_melt,aes(factor(condition),y=as.numeric(value))) + 
    geom_bar(stat = &quot;identity&quot;, position = &quot;stack&quot;)
  </code></pre>
  <p>I am certain I must be missing something obvious about how to create values for the counts, but I'm not sure what. Any guidance is appreciated :)</p>