Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consistent colouring in `ggplot2` when factor levels are (sometimes) missing using `scale_colour_manual` or an alternative

Tags:

r

colors

ggplot2

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)

enter image description here

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)

enter image description here

How can I achieve a consistent colouring that is independent from missing factor levels?

like image 812
symbolrush Avatar asked Oct 15 '25 04:10

symbolrush


2 Answers

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.

like image 170
Phil S Avatar answered Oct 17 '25 21:10

Phil S


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.

like image 23
Marius Avatar answered Oct 17 '25 19:10

Marius