For a bar graph, I want to stack the bar components vertically in a custom order.  In the example below, the variable that should control the stacking order is “mechanism.”  The intended stacking order is neither alphabetical order nor the order in which the dataframe’s rows are sequenced.  Not all bars have all mechanisms. I manually set the sequence and use the sequence as levels for a factor.  The entirely missing factor level doesn't get handled right, for which a solution would be welcome but I can work around that.

To add text labels indicating each component’s magnitude, I calculate the labels’ y height values and add a geom\_text.  I want the color of the text to be a consistent contrast to the color of the bar component:  white text on blue bar components, etc.  So I assign geom\_text's color to the same variable as the geom\_bar’s fill.  But the intended color pairings for background and text do not remain matched.  Ideas please?? (thank you to the editor who inserted the picture of the messed up graph.)

The desired outcome is that each bar component whose mechanism is "hot" would have red fill annotated with black text. Each component whose mechanism is "urban" would have tan4 fill annotated with grey50 text.

I also tried assigning the color without making the mapped variable into a factor (example option 1), as in [geom\_text() & color gradient](https://stackoverflow.com/questions/47315822/geom-text-color-gradient).

```r
library(ggplot2)
m.data <- data.frame(m.model = factor(c(1, 1, 2, 2, 4)),
   mechanism = c("urban", "hot", "hot", "urban", "urban"),
   bar.height = 1:5,
   y.for.text = c(.5, 2, 1.5, 5, 2.5))
mechanism.order <- c("hot", "dry", "urban")
mechanism.colors <- c("red", "blue", "tan4")
text.colors <- c("black", "white", "grey50")

m.map <- ggplot() +
   geom_bar(data = m.data, aes(x = m.model, y = bar.height, 
      fill = factor(mechanism, levels = mechanism.order)), 
      stat = "identity") +
   scale_fill_manual(values = mechanism.colors) +
   scale_color_manual(values = text.colors) 

# option 1:
m.map +    
   geom_text(data = m.data, aes(x = m.model, y = y.for.text,
      color = mechanism, label = bar.height)) 

# option 2:
m.map +    
   geom_text(data = m.data, aes(x = m.model, y = y.for.text,
      color = factor(mechanism, levels = mechanism.order), 
      label = bar.height))

```

![](https://i.stack.imgur.com/NVF0t.jpg)