title: Multirow axis labels with nested grouping variables for stacked bar plot in - R
body: <p>I would like to make a stacked bar graph with multiple categories with a nested labelled X-axis using ggplot, similar to the one I've made using Excel as shown here. </p>
  
  <p><a href="https://i.stack.imgur.com/J9vE6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J9vE6.png" alt="enter image description here"></a></p>
  
  <p>I tried using the example given <a href="https://stackoverflow.com/questions/18165863/multirow-axis-labels-with-nested-grouping-variables">here</a> for an unstacked bar graph using <code>facet_wrap()</code> resulting in this</p>
  
  <p><a href="https://i.stack.imgur.com/fYr61.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fYr61.png" alt="enter image description here"></a></p>
  
  <p>but as you see this code spreads out the columns very far apart, and doesn't cluster the stacked bars with category levels (i.e. 2017,2030,2040) next to each other.</p>
  
  <p>I also tried using the example <a href="https://stackoverflow.com/questions/24645961/printing-group-x-axis-labels-in-stacked-ggplot2-bar-plot">here</a> but this only allows for one X-category level, not two.</p>
  
  <p>I've added some example code below (reduced from the original but with the same intent).</p>
  
  <p>Some help would be great!</p>
  
  <pre><code>data1 &lt;- read.table(text = "Property Category Datetime Value
                 PoolRevenue Nuclear 2017 73
                 PoolRevenue Nuclear 2030 75
                 PoolRevenue CCGT 2017 57
                 PoolRevenue CCGT 2030 45
                 PoolRevenue Hydro 2017 23
                 PoolRevenue Hydro 2030 28
                 ReservesRevenue Nuclear 2017 34
                 ReservesRevenue Nuclear 2030 37
                 ReservesRevenue CCGT 2017 34
                 ReservesRevenue CCGT 2030 9
                 ReservesRevenue Hydro 2017 23
                 ReservesRevenue Hydro 2030 36
                 OtherRevenue Nuclear 2017 96
                 OtherRevenue Nuclear 2030 98
                 OtherRevenue CCGT 2017 55
                 OtherRevenue CCGT 2030 53
                 OtherRevenue Hydro 2017 60
                 OtherRevenue Hydro 2030 65", header=TRUE)
  data2 &lt;- read.table(text = "Property Category Datetime Value
                  GenCost Nuclear 2017 -10
                  GenCost Nuclear 2030 -20
                  GenCost CCGT 2017 -15
                  GenCost CCGT 2030 -20
                  GenCost Hydro 2017 -10
                  GenCost Hydro 2030 -15
                  FixedCosts Nuclear 2017 -13
                  FixedCosts Nuclear 2030 -11
                  FixedCosts CCGT 2017 -30
                  FixedCosts CCGT 2030 -12
                  FixedCosts Hydro 2017 -15
                  FixedCosts Hydro 2030 -12", header=TRUE)
  data1$Datetime &lt;- as.factor(data1$Datetime)
  data2$Datetime &lt;- as.factor(data2$Datetime)
  p1 &lt;-ggplot()
  p2 &lt;-  p1+ geom_bar(data=data1,aes_string(x="Datetime",y="Value",fill="Property"),stat="identity", position = position_stack(reverse = TRUE))+guides(fill = guide_legend(reverse=TRUE))
  p3 &lt;-  p2+ geom_bar(data=data2,aes_string(x="Datetime",y="Value",fill="Property"),stat="identity", position = position_stack(reverse = TRUE))+guides(fill = guide_legend(reverse=TRUE))
  p4 &lt;- p3 +facet_wrap(c("Category","Datetime"), strip.position = "bottom", scales = "free_x") + theme(panel.spacing = unit(0, "lines"), strip.background = element_blank(), strip.placement = "outside")
  </code></pre>
  
  <p>(Note, as I have both positive and negative values in my stacked bar graphs, I'm applying them as separate layers, but I don't think this should affect the axes.)</p>