title: How to make a bar-chart by using two variables on x-axis and a grouped variable on y-axis?
body: <p>I hope I asked my question in the right way this time! If not let me know!
  I want to code a grouped bar-chart similary to this one (I just created in paint):
  <a href="https://i.stack.imgur.com/L5qJw.png" rel="nofollow noreferrer">enter image description here</a>
  I created as flipped both it actually doesn't matter if its flipped or not. So, a plot similarly to this will also be very usefull:
  <a href="https://stackoverflow.com/questions/61506408/grouped-barchart-in-r-with-4-variables">Grouped barchart in r with 4 variables</a></p>
  <p>Both the variables, happy and lifesatisfied are scaled values from 0 to 10. Working hours is a grouped value and contains 43+, 37-42, 33-36, 27-32, and &lt;27.</p>
  <p>A very similar example of how my data set looks like (I just changed the values and order, I also have much more observations):</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;">Working hours</th>
  <th style="text-align: center;">happy</th>
  <th style="text-align: center;">lifestatisfied</th>
  <th style="text-align: center;">contry</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;">37-42</td>
  <td style="text-align: center;">7</td>
  <td style="text-align: center;">9</td>
  <td style="text-align: center;">DK</td>
  </tr>
  <tr>
  <td style="text-align: center;">&lt;27</td>
  <td style="text-align: center;">8</td>
  <td style="text-align: center;">8</td>
  <td style="text-align: center;">SE</td>
  </tr>
  <tr>
  <td style="text-align: center;">43+</td>
  <td style="text-align: center;">7</td>
  <td style="text-align: center;">8</td>
  <td style="text-align: center;">DK</td>
  </tr>
  <tr>
  <td style="text-align: center;">33-36</td>
  <td style="text-align: center;">6</td>
  <td style="text-align: center;">6</td>
  <td style="text-align: center;">SE</td>
  </tr>
  <tr>
  <td style="text-align: center;">37-42</td>
  <td style="text-align: center;">7</td>
  <td style="text-align: center;">5</td>
  <td style="text-align: center;">NO</td>
  </tr>
  <tr>
  <td style="text-align: center;">&lt;27</td>
  <td style="text-align: center;">4</td>
  <td style="text-align: center;">7</td>
  <td style="text-align: center;">NO</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I tried to found similar examples and based on that tried to code the bar chart in the following way but it doesn't work:</p>
  <pre><code>df2 &lt;- datafilteredwomen %&gt;% 
    pivot_longer(cols = c(&quot;happy&quot;, &quot;stflife&quot;), names_to = &quot;var&quot;, values_to = &quot;Percentage&quot;)
  
  ggplot(df2) +
    geom_bar(aes(x = Percentage, y = workinghours, fill = var ), stat = &quot;identity&quot;, position = &quot;dodge&quot;) + theme_minimal()
  </code></pre>
  <p>It give this plot which is not correct/what I want:
  <a href="https://i.stack.imgur.com/l5WmG.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><strong>seocnd try:</strong></p>
  <pre><code>forplot = datafilteredwomen %&gt;% group_by(workinghours, happy, stflife) %&gt;% summarise(count = n()) %&gt;% mutate(proportion = count/sum(count))
  
  ggplot(forplot, aes(workinghours, proportion, fill = as.factor(happy))) + 
    geom_bar(position = &quot;dodge&quot;, stat = &quot;identity&quot;, color = &quot;black&quot;) 
  </code></pre>
  <p>gives this plot:
  <a href="https://i.stack.imgur.com/tXIUU.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><strong>third try - used the ggplot2 builder add-in:</strong></p>
  <pre><code>library(dplyr)
  library(ggplot2)
  
  datafilteredwomen %&gt;%
   filter(!is.na(workinghours)) %&gt;%
   ggplot() +
   aes(x = workinghours, group = happy, weight = happy) +
   geom_bar(position = &quot;dodge&quot;, 
   fill = &quot;#112446&quot;) +
   theme_classic() + scale_y_continuous(labels = scales::percent)
  </code></pre>
  <p>gives this plot:
  <a href="https://i.stack.imgur.com/Dpfme.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>But none of my tries are what I want.. really hope that someone can help me if it's possible!</p>