I have the following sample data. The variable "y" has 4 factor levels. However, only 3 of them appear in the data.
How can I add the missing category "C" to the legend (and not necessarily to the plot)?
library(tidyverse)
# Sample data
n <- 100
df <- data.frame(x = sample(c("grp1","grp2", "grp3", "grp4"), size = n, replace = TRUE),
y = sample(c("A", "B", "D", "E"), size = n, replace = TRUE)) %>%
mutate(x = as.factor(x),
y = factor(y, levels = c("A", "B", "C", "D", "E")))
# Stacked barplot
df %>%
group_by(x) %>%
count(y) %>%
mutate(percent = 1 / sum(n) * n) %>%
ggplot(aes(x = x, y = percent, fill = y)) + geom_col()