Skip to content
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

scale_color_manual and scale_linetype_manual did not work in the dev version 3.3.3.9000 #4479

Closed
tungttnguyen opened this issue May 16, 2021 · 7 comments

Comments

@tungttnguyen
Copy link

Brief description of the problem

Adding scale_color_manual and scale_linetype_manual makes all the lines disappeared. This was not a problem with the CRAN version 3.3.3.

Please let me know if I missed anything obvious. Thanks!

library(tibble)
library(dplyr)
library(ggplot2)
library(scales)

DF <- tribble(
  ~Quality, ~value,   ~x,
  "Good",   34.2, 9.28,
  "Good",     34, 7.97,
  "Good",   33.8, 6.94,
  "Average",   33.5, 9.86,
  "Good",   33.5, 6.08,
  "Average",   33.3, 9.64,
  "Good",   33.3, 5.51,
  "Average",   33.1, 9.42,
  "Good",   33.1, 5.01,
  "Average",   32.9, 9.21,
  "Good",   32.9, 4.57,
  "Average",   32.7, 9.01,
  "Good",   32.7, 4.23,
  "Average",   32.5, 8.81,
  "Good",   32.5, 3.93,
  "Average",   32.3, 8.62,
  "Good",   32.3, 3.71,
  "Average",   32.1, 8.36,
  "Good",   32.2, 3.53,
  "Average",   31.9, 8.04,
  "Good",     32, 3.36,
  "Average",   31.7, 7.61,
  "Good",   31.8, 3.22,
  "Average",   31.5, 6.91,
  "Good",   31.6, 3.09,
  "Average",   31.3, 6.23,
  "Good",   31.4, 2.98,
  "Average",   31.1, 5.54,
  "Good",   31.3, 2.87,
  "Average",     31, 4.97,
  "Good",   31.1, 2.76,
  "Bad",   30.6, 9.65,
  "Average",   30.8, 4.54,
  "Good",   30.9, 2.67,
  "Bad",   30.4, 8.33,
  "Average",   30.6,  4.2,
  "Good",   30.7, 2.59,
  "Bad",   30.2, 7.29,
  "Average",   30.4, 3.89,
  "Good",   30.6, 2.51,
  "Bad",     30,  6.6,
  "Average",   30.2, 3.62,
  "Good",   30.4, 2.43,
  "Bad",   29.9, 6.07,
  "Average",     30, 3.43,
  "Good",   30.3, 2.36,
  "Bad",   29.7, 5.66,
  "Average",   29.9, 3.27,
  "Good",   30.1, 2.29,
  "Bad",   29.5, 5.29,
  "Average",   29.7, 3.13,
  "Good",   29.9, 2.22,
  "Bad",   29.3, 4.94,
  "Average",   29.5, 3.01,
  "Good",   29.8, 2.16,
  "Bad",   29.2, 4.63,
  "Average",   29.3, 2.89,
  "Good",   29.7,  2.1,
  "Bad",     29, 4.35,
  "Average",   29.2, 2.77,
  "Good",   29.5, 2.04,
  "Bad",   28.8, 4.08,
  "Average",     29, 2.67,
  "Good",   29.4, 1.99,
  "Bad",   28.7, 3.83,
  "Average",   28.8, 2.57,
  "Good",   29.2, 1.94,
  "Bad",   28.5, 3.61,
  "Average",   28.7, 2.47,
  "Good",   29.1, 1.89,
  "Bad",   28.3,  3.4,
  "Average",   28.5, 2.38,
  "Good",     29, 1.85,
  "Bad",   28.2, 3.21,
  "Average",   28.4,  2.3,
  "Good",   28.8, 1.81,
  "Bad",     28, 3.04,
  "Average",   28.2, 2.22,
  "Good",   28.6, 1.78,
  "Bad",   27.9, 2.88,
  "Average",   28.1, 2.15,
  "Good",   28.4, 1.75,
  "Bad",   27.8, 2.74,
  "Average",     28, 2.08,
  "Good",   28.2, 1.71,
  "Bad",   27.6,  2.6,
  "Average",   27.8, 2.02,
  "Good",     28, 1.68,
  "Bad",   27.5, 2.49,
  "Average",   27.7, 1.96,
  "Good",   27.8, 1.66,
  "Bad",   27.4, 2.39,
  "Average",   27.5,  1.9,
  "Good",   27.7, 1.63,
  "Bad",   27.2,  2.3,
  "Average",   27.4, 1.85,
  "Good",   27.5, 1.61,
  "Bad",     27, 2.22,
  "Average",   27.2,  1.8,
  "Good",   27.3, 1.59,
  "Bad",   26.8, 2.15,
  "Average",   27.1, 1.75,
  "Good",   27.2, 1.57,
  "Bad",   26.6, 2.08,
  "Average",   26.9, 1.71,
  "Good",     27, 1.54,
  "Bad",   26.4, 2.02,
  "Average",   26.7, 1.67,
  "Good",   26.8, 1.52,
  "Bad",   26.2, 1.97,
  "Average",   26.5, 1.63,
  "Good",   26.6,  1.5,
  "Bad",     26, 1.91,
  "Average",   26.3,  1.6,
  "Good",   26.5, 1.48,
  "Bad",   25.8, 1.86,
  "Average",   26.1, 1.57,
  "Good",   26.3, 1.47,
  "Bad",   25.6, 1.81,
  "Average",   25.9, 1.54,
  "Good",   26.1, 1.45,
  "Bad",   25.5, 1.77,
  "Average",   25.7, 1.51,
  "Good",     26, 1.43,
  "Bad",   25.3, 1.73,
  "Average",   25.5, 1.48,
  "Good",   25.8, 1.42,
  "Bad",   25.1, 1.68,
  "Average",   25.4, 1.45,
  "Good",   25.6,  1.4,
  "Bad",     25, 1.65,
  "Average",   25.2, 1.43,
  "Good",   25.5, 1.38,
  "Bad",   24.8, 1.61,
  "Average",     25,  1.4,
  "Good",   25.3, 1.37,
  "Bad",   24.6, 1.58,
  "Average",   24.8, 1.38,
  "Good",   25.2, 1.36,
  "Bad",   24.5, 1.54,
  "Average",   24.7, 1.36,
  "Good",     25, 1.34,
  "Bad",   24.3, 1.51,
  "Average",   24.5, 1.34,
  "Good",   24.9, 1.33,
  "Bad",   24.2, 1.48,
  "Average",   24.4, 1.32,
  "Good",   24.7, 1.32,
  "Bad",   24.1, 1.46,
  "Average",   24.2,  1.3,
  "Good",   24.6,  1.3,
  "Bad",   23.9, 1.44,
  "Average",   24.1, 1.29,
  "Good",   24.4, 1.29,
  "Bad",   23.8, 1.42,
  "Average",     24, 1.27,
  "Good",   24.3, 1.28,
  "Bad",   23.7,  1.4,
  "Average",   23.9, 1.26,
  "Good",   24.1, 1.26,
  "Bad",   23.6, 1.38,
  "Average",   23.8, 1.25,
  "Good",     24, 1.25,
  "Bad",   23.5, 1.37,
  "Average",   23.7, 1.24,
  "Good",   23.8, 1.24,
  "Bad",   23.4, 1.35,
  "Average",   23.6, 1.23,
  "Good",   23.7, 1.23,
  "Bad",   23.2, 1.33,
  "Average",   23.4, 1.22,
  "Good",   23.5, 1.22,
  "Bad",   23.1, 1.32,
  "Average",   23.3, 1.21,
  "Good",   23.3, 1.21,
  "Average",   23.2,  1.2,
  "Good",   23.1,  1.2
)

Set colors and line types manually

quality_list <- DF %>%
  distinct(Quality) %>%
  pull()
quality_list
#> [1] "Good"    "Average" "Bad"

manual_scale_colors <- setNames(c("#888888", "#332288", "#88CCEE"), quality_list)
manual_scale_colors
#>      Good   Average       Bad 
#> "#888888" "#332288" "#88CCEE"
manual_scale_linetype <- setNames(c("solid", "dashed", "dotted"), quality_list)
manual_scale_linetype
#>     Good  Average      Bad 
#>  "solid" "dashed" "dotted"

Plot

log_breaks <- function(maj, radix = 10) {
  function(x) {
    minx <- floor(min(logb(x, radix), na.rm = TRUE)) - 1
    maxx <- ceiling(max(logb(x, radix), na.rm = TRUE)) + 1
    n_major <- maxx - minx + 1
    major_breaks <- seq(minx, maxx, by = 1)
    if (maj) {
      breaks <- major_breaks
    } else {
      steps <- logb(1:(radix - 1), radix)
      breaks <- rep(steps, times = n_major) +
        rep(major_breaks, each = radix - 1)
    }
    radix^breaks
  }
}

scale_x_log_eng <- function(..., radix = 10) {
  scale_x_continuous(...,
    trans = log_trans(radix),
    breaks = log_breaks(TRUE, radix),
    minor_breaks = log_breaks(FALSE, radix)
  )
}

str_pad_custom <- function(labels) {
  new_labels <- as.character(1/as.numeric(labels))
  return(new_labels)
}

p1 <- ggplot(DF, aes(x = x,
                     y = value,
                     linetype = Quality,
                     color = Quality)) +
  geom_line(size = 1) +
  scale_x_log_eng(limits = c(1, 10),
                  expand = c(0, 0),
                  labels = str_pad_custom) +
  scale_y_continuous(labels = scales::comma, expand = expansion(mult = c(0, 0.1))) +
  annotation_logticks(sides = 'tb') +
  theme_bw(base_size = 16) 
p1

Adding manual color scale

p1 +
  scale_color_manual("", values = manual_scale_colors)

Adding both manual color and linetype scales

p1 +
  scale_color_manual("", values = manual_scale_colors) +
  scale_linetype_manual("", values = manual_scale_linetype)

Here is the plot result generated using CRAN v3.3.3

Created on 2021-05-16 by the reprex package (v2.0.0)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  version  R version 4.0.3 (2020-10-10)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  ctype    English_United States.1252  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version    date       lib source                            
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.0)                    
#>  backports     1.2.1      2020-12-09 [1] CRAN (R 4.0.3)                    
#>  cli           2.5.0      2021-04-26 [1] CRAN (R 4.0.5)                    
#>  colorspace    2.0-1      2021-05-04 [1] CRAN (R 4.0.5)                    
#>  crayon        1.4.1      2021-02-08 [1] CRAN (R 4.0.3)                    
#>  curl          4.3.1      2021-04-30 [1] CRAN (R 4.0.3)                    
#>  DBI           1.1.1      2021-01-15 [1] CRAN (R 4.0.3)                    
#>  digest        0.6.27     2020-10-24 [1] CRAN (R 4.0.2)                    
#>  dplyr       * 1.0.6      2021-05-05 [1] CRAN (R 4.0.5)                    
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.0.3)                    
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.0.0)                    
#>  fansi         0.4.2      2021-01-15 [1] CRAN (R 4.0.3)                    
#>  farver        2.1.0      2021-02-28 [1] CRAN (R 4.0.3)                    
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.0.3)                    
#>  generics      0.1.0      2020-10-31 [1] CRAN (R 4.0.2)                    
#>  ggplot2     * 3.3.3.9000 2021-05-16 [1] Github (tidyverse/ggplot2@c9adeed)
#>  glue          1.4.2      2020-08-27 [1] CRAN (R 4.0.2)                    
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 4.0.0)                    
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.0.5)                    
#>  htmltools     0.5.1.1    2021-01-22 [1] CRAN (R 4.0.3)                    
#>  httr          1.4.2      2020-07-20 [1] CRAN (R 4.0.2)                    
#>  knitr         1.33       2021-04-24 [1] CRAN (R 4.0.5)                    
#>  labeling      0.4.2      2020-10-20 [1] CRAN (R 4.0.3)                    
#>  lifecycle     1.0.0      2021-02-15 [1] CRAN (R 4.0.4)                    
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.0.3)                    
#>  mime          0.10       2021-02-13 [1] CRAN (R 4.0.4)                    
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 4.0.0)                    
#>  pillar        1.6.0      2021-04-13 [1] CRAN (R 4.0.5)                    
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.0.0)                    
#>  purrr         0.3.4.9000 2021-05-16 [1] Github (tidyverse/purrr@5aca9df)  
#>  R6            2.5.0      2020-10-28 [1] CRAN (R 4.0.2)                    
#>  reprex        2.0.0      2021-04-02 [1] CRAN (R 4.0.5)                    
#>  rlang         0.4.11     2021-04-30 [1] CRAN (R 4.0.3)                    
#>  rmarkdown     2.8        2021-05-07 [1] CRAN (R 4.0.5)                    
#>  scales      * 1.1.1      2020-05-11 [1] CRAN (R 4.0.0)                    
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.0.0)                    
#>  stringi       1.5.3      2020-09-09 [1] CRAN (R 4.0.3)                    
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.0.0)                    
#>  styler        1.4.1      2021-03-30 [1] CRAN (R 4.0.5)                    
#>  tibble      * 3.1.1      2021-04-18 [1] CRAN (R 4.0.5)                    
#>  tidyselect    1.1.1      2021-04-30 [1] CRAN (R 4.0.5)                    
#>  utf8          1.2.1      2021-03-12 [1] CRAN (R 4.0.5)                    
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.0.3)                    
#>  withr         2.4.2      2021-04-18 [1] CRAN (R 4.0.5)                    
#>  xfun          0.23       2021-05-15 [1] CRAN (R 4.0.3)                    
#>  xml2          1.3.2      2020-04-23 [1] CRAN (R 4.0.0)                    
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.0.0)                    
#> 
@teunbrand
Copy link
Collaborator

A more minimal reprex:

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

ggplot(df, aes(x, y, colour = group, linetype = group)) +
  geom_line() +
  scale_colour_manual(
    "",
    values = setNames(c("blue", "green", "red"), groups)
  ) +
  scale_linetype_manual(
    "",
    values = setNames(c("solid", "dashed", "dotted"), groups)
  )

The problem appears to be related to how the first unnamed argument is passed around. If you explicitly set name = "", it works as intended.

ggplot(df, aes(x, y, colour = group, linetype = group)) +
  geom_line() +
  scale_colour_manual(
    name = "",
    values = setNames(c("blue", "green", "red"), groups)
  ) +
  scale_linetype_manual(
    name = "",
    values = setNames(c("solid", "dashed", "dotted"), groups)
  )

Created on 2021-05-16 by the reprex package (v1.0.0)

@yutannihilation
Copy link
Member

Thanks, confirmed. Now the first unnamed argument is absorbed by limits arg. Probably after this change: #4471

@thomasp85
Maybe limits should be exposed explicitly in the signature of scale_*_manual() so that we can pass it to manual_scale() inside?

@thomasp85
Copy link
Member

Fixed in the release candidate

@yutannihilation
Copy link
Member

@thomasp85
I don't see any fix in v3.3.4-rc branch. What do you mean by "the release candidate"? May I keep this issue open until the commit gets included in the RC branch (or master) to make sure it's really fixed?

https://github.com/tidyverse/ggplot2/commits/v3.3.4-rc

@thomasp85
Copy link
Member

forgot to push - it should be there now

@yutannihilation
Copy link
Member

Confirmed, thanks! 7022d31

@tungttnguyen
Copy link
Author

Thank you all for the help!

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

4 participants