Force ggplot legend to show all categories when no values are present [duplicate]
You should be able to use drop = FALSE
within scale_fill_manual
. That is,
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"),
drop = FALSE)
For more information, see ?discrete_scale