bthebread
bthebread

Reputation: 305

Hide unused levels in ggplot legend

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

Answers (1)

bthebread
bthebread

Reputation: 305

OK; the related issue 4511 gives the answer. Setting limits = force in scale_fill_manual did it.

Upvotes: 12

Related Questions