Skip to content

unused factor levels are not omitted from the legend #4662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Oravishayrizi opened this issue Nov 7, 2021 · 1 comment
Closed

unused factor levels are not omitted from the legend #4662

Oravishayrizi opened this issue Nov 7, 2021 · 1 comment

Comments

@Oravishayrizi
Copy link

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

@yutannihilation
Copy link
Member

Thanks for reporting. This is already fixed by #4619 in the dev version. Sorry for inconvenience until the next release.

library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.3.5.9000'

# 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) #

Created on 2021-11-07 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants