Skip to content

Error when combining scale_color_manual and guide_legend (override.aes) in v3.3.4 #4511

Closed
@tungttnguyen

Description

@tungttnguyen

With the reprex below, I got the following error when changing the alpha and size values using guide_legend (override.aes). Note that the current CRAN version (v3.3.3) worked without any error.

Error in `[[<-.data.frame`(`*tmp*`, i, value = c(0.6, 0.6)) : 
  replacement has 2 rows, data has 3

Reprex (data taken from this issue)

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

groups <- LETTERS[1:3]

df <- data.frame(
  x = rep(1:10, 3),
  y = rnorm(30),
  group = rep(groups, each = 10)
)

Set up manual color scale

my_color <- setNames(c("blue", "green", "red"), unique(df$group))
my_color
#>       A       B       C 
#>  "blue" "green"   "red"

Filter out group "A"

df1 <- df[df$group != "A", ]
df1
#>     x             y group
#> 11  1  0.7970884438     B
#> 12  2  1.0696606257     B
#> 13  3 -0.4524050451     B
#> 14  4  0.1683655197     B
#> 15  5  0.0723548494     B
#> 16  6  0.2621733369     B
#> 17  7 -0.6167289461     B
#> 18  8  0.3669677647     B
#> 19  9 -0.6197559708     B
#> 20 10 -0.6741798000     B
#> 21  1 -0.9238588784     C
#> 22  2  1.0908007314     C
#> 23  3 -0.8173714908     C
#> 24  4  0.3555677658     C
#> 25  5  0.0862835605     C
#> 26  6 -0.1650330989     C
#> 27  7 -0.6288903181     C
#> 28  8 -0.2195618380     C
#> 29  9  1.1759632572     C
#> 30 10  0.0005726088     C
n_group <- length(unique(df1$group))
n_group
#> [1] 2

p1 <- ggplot(df1, aes(x, y, colour = group)) +
  geom_point(size = 3)
p1

Legend for group "A" shows up even though we don't have it anymore (this wasn't the problem with v3.3.3 CRAN)

p1 +
  scale_colour_manual(
    "",
    values = my_color
  )

p1 +
  scale_colour_manual(
    "",
    values = my_color
  ) +
  guides(color = guide_legend(title = "",
                              override.aes = list(alpha  = rep(0.6, n_group),
                                                  size   = rep(6,   n_group)),
                              order = 1),
         stroke = "none",
         shape  = "none")
#> Error in `[[<-.data.frame`(`*tmp*`, i, value = c(0.6, 0.6)): replacement has 2 rows, data has 3

Created on 2021-06-14 by the reprex package (v2.0.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions