title: Force ggplot legend to show all categories when no values are present
body: <p>I am trying to force ggplot to show the legend and fix the colors for a factor even if no value in a range is present.</p>
  
  <p>In the reproducible example below, Figure 1 has at least one value in each range of the variable X1 and plots as desired. Each legend label is plotted and matches the desired color.</p>
  
  <p>In example 2, the variable Y1 does not have a value in each of the ranges that are created. As a result, the plot only shows the first 4 legend labels and uses the first 4 colors.</p>
  
  <p>Is there a way to plot this figure that forces ggplot to show all eight legend labels and fix the colors so that cat1 values are always red, cat2 values are always blue, etc.</p>
  
  <p>I have tried everything I can think of without success.</p>
  
  <p>-- Reproducible example --</p>
  
  <pre><code>set.seed(45678)
  dat &lt;- data.frame(Row = rep(x = LETTERS[1:5], times = 10), 
                    Col = rep(x = LETTERS[1:10], each = 5),
                    Y = rnorm(n = 50, mean = 0, sd = 0.5),
                    X = rnorm(n = 50, mean = 0, sd = 2))
  
  library(ggplot2)
  library(RColorBrewer)
  library(dplyr)
  
  dat &lt;- dat %&gt;% mutate(Y1 = cut(Y, breaks = c(-Inf,-3:3,Inf)),
                        X1 = cut(X, breaks = c(-Inf,-3:3,Inf)))
  
  # Figure 1
  ggplot(data =  dat, aes(x = Row, y = Col)) +
    geom_tile(aes(fill = X1), color = "black") +
    scale_fill_manual(values = c("red", "blue", "green", "purple", "pink", "yellow", "orange", "blue"),
                      labels = c("cat1", "cat2", "cat3", "cat4", "cat5", "cat6", "cat7", "cat8"))
  
  # Figure 2
  ggplot(data =  dat, aes(x = Row, y = Col)) +
    geom_tile(aes(fill = Y1), color = "black") +
    scale_fill_manual(values = c("red", "blue", "green", "purple", "pink", "yellow", "orange", "blue"),
                      labels = c("cat1", "cat2", "cat3", "cat4", "cat5", "cat6", "cat7", "cat8"))
  </code></pre>