Reputation: 305
Recently I was forced to update R to version 4.05, and since then I have an unexpected behaviour in ggplot. I want ggplot to hide unused factor levels in the legend. This should work with the following code. And it did work in my older R version.
Does anyone know, what I have to change in R 4.05? In this example I expect only to see "VC" (red) in the legend.
library(tidyverse)
mycols <- setNames(c('red' , 'blue') ,
c('VC' , 'OJ'))
ToothGrowth %>% filter(supp == 'VC') %>%
ggplot(aes(x=len , y = supp)) +
geom_col(aes(fill = supp)) +
scale_fill_manual(values = mycols , drop = TRUE)
Upvotes: 9
Views: 2979
Reputation: 305
OK; the related issue 4511 gives the answer. Setting limits = force
in scale_fill_manual did it.
Upvotes: 12