Skip to content

Unexpected behaviour of colors in geom_line() #6533

@MathiasAmbuehl

Description

@MathiasAmbuehl

This issue is related to coloring line segments individually when using geom_line() and scale_color_identity().

I' m drawing a simple ggplot:

library(ggplot2)
(d <- data.frame(x = 1:3, col = c("blue", "green", "red")))
#>   x   col
#> 1 1  blue
#> 2 2 green
#> 3 3   red
ggplot(d, aes(x, x, color = col, group = 1)) +
    geom_line(lwd = 1) + 
    scale_color_identity()

Created on 2025-06-27 with reprex v2.1.1

This makes sense to me. Segments are colored using color of the starting point of the segments, and consequently the last color is ignored.

Now, I replace "red" by an NA:

d1 <- data.frame(x = 1:3, col = c("blue", "green", NA))
ggplot(d1, aes(x, x, color = col, group = 1)) +
	geom_line(lwd = 1) + 
	scale_color_identity()

I get the same plot (not shown), with a warning:

Warning message:
Removed 1 row containing missing values or values outside the scale range (`geom_line()`). 

The warning may be considered unnecessary, but it's ok, since there is an NA the color column.

Now I duplicate the data and redraw, faceting by group:
In the right panel, the green segment is missing, which does not make any sense to me.

library(ggplot2)
d2 <- data.frame(x = 1:3, col = c("blue", "green", NA)) |> 
  tidyr::crossing(group = c("A", "B"))
d2
#> # A tibble: 6 × 3
#>       x col   group
#>   <int> <chr> <chr>
#> 1     1 blue  A    
#> 2     1 blue  B    
#> 3     2 green A    
#> 4     2 green B    
#> 5     3 <NA>  A    
#> 6     3 <NA>  B
ggplot(d2, aes(x, x, color = col, group = 1)) +
    facet_wrap(~ group) + 
    geom_line(lwd = 2) + 
    scale_color_identity()
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_line()`).

Created on 2025-06-27 with reprex v2.1.1

NB: Probably, using geom_segment() may be the better choice here...

Session info:

sessioninfo::session_info()
#> Warning in system2("quarto", "-V", stdout = TRUE, env = paste0("TMPDIR=", :
#> running command '"quarto"
#> TMPDIR=C:/Users/mam/AppData/Local/Temp/Rtmp6zS3jc/file67105eff162c -V' had
#> status 1
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.0 (2025-04-11 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  German_Switzerland.utf8
#>  ctype    German_Switzerland.utf8
#>  tz       Europe/Stockholm
#>  date     2025-06-27
#>  pandoc   3.4 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>  quarto   NA @ C:\\PROGRA~1\\RStudio\\RESOUR~1\\app\\bin\\quarto\\bin\\quarto.exe
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] CRAN (R 4.5.0)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.5.0)
#>  evaluate      1.0.3   2025-01-10 [1] CRAN (R 4.5.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.5.0)
#>  fs            1.6.6   2025-04-12 [1] CRAN (R 4.5.0)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.5.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.5.0)
#>  knitr         1.50    2025-03-16 [1] CRAN (R 4.5.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.5.0)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.5.0)
#>  rlang         1.1.6   2025-04-11 [1] CRAN (R 4.5.0)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.5.0)
#>  rstudioapi    0.17.1  2024-10-22 [1] CRAN (R 4.5.0)
#>  sessioninfo   1.2.3   2025-02-05 [1] CRAN (R 4.5.0)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.5.0)
#>  xfun          0.52    2025-04-02 [1] CRAN (R 4.5.0)
#>  yaml          2.3.10  2024-07-26 [1] CRAN (R 4.5.0)
#> 
#>  [1] C:/R/R-4.5.0/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Created on 2025-06-27 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorlayers 📈

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy