Dear all,
Before aking my question, I went through all the web discussions about scale_colour_manual(). My impression is that the problem is related to the new version of ggplot (version 3.5.5) as before it worked for me.
As far as I understand from the help function (and from the discussions on the web), with scale_colour_manual() the unused factor levels are omitted from the legend.
This does not work (any more) for me, even if I set drop = TRUE as illustrated in the example below. I am using a recent version of ggplot2 (3.3.5).
In this example the legend still contains a key for "Y" (red) although the factor (the variable Extra) in the dataset only contains "N".
library(ggplot2); library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
theme_set(theme_bw(base_size = 15))
## a local copy of the BOD dataset
## time points larger than five days are labelled as extra
tib_BOD <- BOD %>%
as_tibble() %>%
mutate(Extra = factor(ifelse(Time > 5, "Y", "N")))
tib_BOD
#> # A tibble: 6 x 3
#> Time demand Extra
#> <dbl> <dbl> <fct>
#> 1 1 8.3 N
#> 2 2 10.3 N
#> 3 3 19 N
#> 4 4 16 N
#> 5 5 15.6 N
#> 6 7 19.8 Y
## a subset without Extra == "Y"
tib_BOD_noExtra <- tib_BOD %>% filter(Extra == "N")
## named vector to define the colours
fcol_Extra <- c(N = "blue", Y = "red")
## the legend contains the "Y" although it is not used
## drop = TRUE is not required as this is the default
ggplot(tib_BOD_noExtra) +
geom_point(aes(x = Time, y = demand, colour = Extra), size = 5) +
scale_colour_manual(values = fcol_Extra, drop = TRUE)
Created on 2021-07-03 by the reprex package (v2.0.0)