Skip to content

unused factor levels are not omitted from the legend #4662

Closed
@Oravishayrizi

Description

@Oravishayrizi

Hello, I came across an unexpected behavior of scale_shape_manual that is not dropping missing values from the legend.
Explicitly using the drop=TRUE argument didn't make any difference on version 3.3.5 (nor version 3.3.4).

In version 3.3.3 the plot drops unused levels. I couldn't find any relevant change in the changelog, I may have missed it, but perhaps it's a bug.

require(remotes)
remotes::install_version("ggplot2", version = "3.3.3", repos = "http://cran.us.r-project.org",quiet = TRUE)
library(ggplot2)

# Dict
irisColors <-
    setNames( c('red', 'forestgreen', 'blue'),
             levels(iris$Species)  )

irislabel <-
    setNames( c('A', 'B', 'C'),
              levels(iris$Species)  )

irisshape <-
    setNames( c(15, 16,17),
              levels(iris$Species) )

# 3.3.3 output

ggplot(data = iris[iris$Species!="virginica",],aes(x = Sepal.Length,y = Sepal.Width,color = Species,shape=Species)) +
    geom_point() +
    scale_color_manual(values = irisColors,labels=irislabel)+
    scale_shape_manual(values = irisshape,labels=irislabel) 
remove.packages("ggplot2")
rstudioapi::restartSession()

image

# 3.3.5 output
install.packages("ggplot2",quiet = TRUE)
library(ggplot2)

# Dict
irisColors <-
    setNames( c('red', 'forestgreen', 'blue'),
              levels(iris$Species)  )

irislabel <-
    setNames( c('A', 'B', 'C'),
              levels(iris$Species)  )

irisshape <-
    setNames( c(15, 16,17),
              levels(iris$Species) )

ggplot(data = iris[iris$Species!="virginica",],aes(x = Sepal.Length,y = Sepal.Width,color = Species,shape=Species)) +
    geom_point() +
    scale_color_manual(values = irisColors,labels=irislabel)+ # Also when I explicit state: drop=TRUE
    scale_shape_manual(values = irisshape,labels=irislabel) #

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions