When you have less colours defined with scale_fill_manual
than levels in a factor, ggplot2
complains with this error message :
# Basic definition of the plot
plot <- ggplot(s4r, aes(x=DIM, y=nbexpress, fill=DIM))
# Printing plot and options
plot + geom_bar(stat="identity", show_guide=FALSE) +
scale_fill_manual(values=c("#CC0000", "#006600", "#669999", "#00CCCC",
"#660099", "#CC0066", "#FF9999", "#FF9900")
Shows:
Error: Insufficient values in manual scale. 10 needed but only 8 provided.
How to avoid this error? It is especially important to me because I work on a server with dynamic data and R embedded in a website CMS and don't want the graphs to fail when there are some wrong levels (this may happen, until we have corrected the database).
So far I've found a workaround (see my answer) but I'm wondering if there is any solution more elegant.