I have a df
and a colour palette cp
:
df <- data.frame(x = c(1,2,3), y = c(1,1,1), col = c(1,2,3),
label = c("one", "two", "three"))
cp <- c("#9986A5", "#79402E", "#0F0D0E")
I can plot the labels with the corresponding colour from my cp
with:
library(ggplot2)
ggplot(df, aes(x, y)) + geom_text(aes(label = label, color = as.factor(col)))
+ scale_colour_manual(values = cp)
However, when there are factor levels missing, the colouring gets inconsistent:
df$col <- c(1,1,3)
ggplot(df, aes(x, y)) + geom_text(aes(label = label, color = as.factor(col)))
+ scale_colour_manual(values = cp)
How can I achieve a consistent colouring that is independent from missing factor levels?
I found the use of drop = FALSE
in scale ... manual calls to be really helpful with the problem of unrepresented factor levels leading to inconsistent colour scales.
If you use a named vector to specify your colour palette, levels will be matched up with colours consistently:
cp <- c(`1` = "#9986A5", `2` = "#79402E", `3` = "#0F0D0E")
You have to quote the numbers to get them to work as names for the vector.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With