rggplot2

ggplot scale_color_manual Not putting the colors in the legend


I am trying to make a graph that has several bins that determine the color. This is done in a bulk script, so not all of the graphs will have all of the bins, but I want the legend to show everything, even the bins that are not represented in the data. However, when I make the graph even though the legend shows all of the bins, it does not include the color for the ones that are not represented in the data.

I am using ggplot 3.5.1 . This is a new problem on an old script, I was not having this problem several months ago. I reverted my pacakge to 3.4.4 and it worked as expected. However, this is not a solution for me as I need to use the most up to date package as per policy. Is there something I can do to fix this error?

Below is some simple reproducible code that produced the same error for me. The lvls_expand() made c show up in the legend, but it is still not appearing with color. I also tried using breaks and labels in the scale_color_manual() call but this had the same reuslt.

library(ggplot2)
library(forcats) ## For lvls_expand
Data <- data.frame(x = c(1,2,3,4), y = c(4:1), class = c('a','b','a','b'))

ggplot(Data, aes(x=x, y=y, color = lvls_expand(as.factor(class),c('a','b','c')))) + geom_point() +
 scale_color_manual(values = c(a = 'red', b = 'blue', c = 'green'), 
                    name = "Legend", drop=FALSE)

Output

There is no color shown for c


Solution

  • It is documented as "drop: Should unused factor levels be omitted from the scale? The default, TRUE, uses the levels that appear in the data; FALSE includes the levels in the factor. Please note that to display every level in a legend, the layer should use show.legend = TRUE."

    So, add show.legend = TRUE could be the solution:

    ggplot(Data, aes(x=x, y=y, color = lvls_expand(as.factor(class),c('a','b','c')))) + 
      geom_point(show.legend = TRUE) +
      scale_color_manual(values = c(a = 'red', b = 'blue', c = 'green'), 
                         name = "Legend", drop=FALSE)
    

    enter image description here