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

group aesthetic not working when color aesthetic also specified #5275

Closed
mkoohafkan opened this issue Apr 18, 2023 · 3 comments
Closed

group aesthetic not working when color aesthetic also specified #5275

mkoohafkan opened this issue Apr 18, 2023 · 3 comments

Comments

@mkoohafkan
Copy link

mkoohafkan commented Apr 18, 2023

I'm pretty sure this is a regression as I used to make plots like this all the time (e.g., plotting traces of ensemble forecasts). It seems that specifying the group aesthetic overrides, rather than being combined with, other aesthetics such as color.

Reprex:

library(ggplot2)
library(dplyr)

# function to generate traces
fake_data = function() {
  bind_rows(
    tibble(x = 1:10, y = sort(rnorm(10))),
    tibble(x = 1:10, y = sort(rnorm(10))),
    tibble(x = 1:10, y = sort(rnorm(10))),
    .id = "trace"
  )
}

# works as expected
traces = fake_data()
ggplot(traces) + aes(x = x, y = y, color = "v1", group = trace) + geom_line()

a

# jumbled result

multi_traces= bind_rows(
    "v1" = traces,
    "v2" = fake_data(),
    .id = "version"
)

ggplot(multi_traces) + aes(x = x, y = y, color = version, group = trace) + geom_line()

b

# displays correctly with facets
ggplot(multi_traces) + aes(x = x, y = y, color = version, group = trace) + geom_line() + facet_wrap(vars(version))

c

# workaround: use interaction() in group specification
ggplot(multi_traces) + aes(x = x, y = y, color = version, group = interaction(version, trace)) + geom_line()

Session Info:

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)  
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  
[2] LC_CTYPE=English_United States.utf8    
[3] LC_MONETARY=English_United States.utf8 
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] dplyr_1.1.1   ggplot2_3.4.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10       magrittr_2.0.3    tidyselect_1.2.0  munsell_0.5.0
 [5] colorspace_2.1-0  R6_2.5.1          ragg_1.2.5        rlang_1.1.0
 [9] fansi_1.0.4       tools_4.2.2       grid_4.2.2        gtable_0.3.3
[13] utf8_1.2.3        cli_3.6.1         withr_2.5.0       systemfonts_1.0.4
[17] tibble_3.2.1      lifecycle_1.0.3   textshaping_0.3.6 farver_2.1.1
[21] later_1.3.0       vctrs_0.6.1       glue_1.6.2        labeling_0.4.2   
[25] compiler_4.2.2    pillar_1.9.0      generics_0.1.3    scales_1.2.1
[29] httpgd_1.3.1      jsonlite_1.8.4    pkgconfig_2.0.3
@teunbrand
Copy link
Collaborator

Hi there, thanks for the report. This behaviour has at least existed since ggplot2 3.0.0, so this has been around for 5 years now. A colour is allowed to vary within a group, so inferring grouping from colours would break quite some code out there. I'm inclined to think that this is not a bug.

@clauswilke
Copy link
Member

The "workaround" you list is the correct way of dealing with this situation. If you manually set the group aesthetic you have to set it exactly how you want it. You can't expect ggplot to take your group assignment and somehow modify it based on other aesthetics you have set. That makes no sense.

@mkoohafkan
Copy link
Author

You can't expect ggplot to take your group assignment and somehow modify it based on other aesthetics you have set

I mean, that's how it works with aesthetics in general---my misunderstanding was in thinking group was an additional aesthetic rather than the explicit way to define the group structure implicitly set by color/linetype/etc. The documentation makes this clear so I must be misremembering how I used the group aesthetic in the past. Sorry about that!

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

3 participants