1

is it possible to have multiple color scales for different layers?

I emulated what I would like to archive by using a fill aesthetic for a geom_point with shape=21 in the example below.

library(tidyverse)

test <- tibble(
  x=c(1:4, 1:4),
  y=c(1:4, 2:5),
  a=factor(c("a", "a", "b", "b", "a", "a", "b", "b")),
  A=factor(c("A", "A", "A", "A", "B", "B", "B", "B"))
)

ggplot(test, aes(x=x, y=y)) + 
  geom_line(aes(colour=A), size=2) +
  geom_point(aes(fill=a), shape=21, colour=rgb(1,1,1,0), size=4) + 
  scale_fill_manual(
    values = c(a="red", b="blue"),
  ) + 
  scale_colour_manual(
    values = c(A="green", B="yellow"),
  )

Why is this useful? An example would be: you have a quantity (y) measured over time (x) for different measurement units (first color scale) using a different method of measurement (second color scale)

In this case I can do with the workaround in the example but I think this is interesting in general.

Also of course having different scales for different layers does not make sense for all layers, scaling one layer's x axis logarithmically an the other one's linearly would not make sense in the most cases.


Edit: I can only use more or less stable packages, anything that might no longer be supported or changes its interface often does not work.

tjebo
  • 21,977
  • 7
  • 58
  • 94
snaut
  • 2,261
  • 18
  • 37
  • 1
    google "several scales for same aesthetic ggplot" - plenty of threads. Check the ggnewscale package, I guess that's what you want. – tjebo Dec 07 '21 at 10:42
  • 1
    Does this answer your question? [How to have different scales the same aesthetic (color) in different geoms?](https://stackoverflow.com/questions/56166901/how-to-have-different-scales-the-same-aesthetic-color-in-different-geoms) – tjebo Dec 07 '21 at 10:43
  • the above is just one of those – tjebo Dec 07 '21 at 10:43
  • Thanks for your replies, the packages look good but both packages state in their first paragraph that they are highly experimental, which disqualifies them for my current usecase. – snaut Dec 07 '21 at 10:53
  • 1
    ggnewscale is on CRAN, this makes it fairly solid – tjebo Dec 07 '21 at 10:54
  • 1
    If you're worried about reproducibility of your code, check the [groundhog package](https://groundhogr.com) - it allows you to reproduce your code based on the exact package versions when you have created the script. – tjebo Dec 07 '21 at 10:56
  • Being on CRAN does not make the interfaces of the package stable. Yes it will unlikely give errors in the current form, yes it will be available, but updates can still break things. I tried packages like packrat but there were always problems due to our samba mount not supporting symlinks. – snaut Dec 07 '21 at 11:01
  • Does this answer your question? [How to set multiple legends / scales for the same aesthetic in ggplot2?](https://stackoverflow.com/questions/17642190/how-to-set-multiple-legends-scales-for-the-same-aesthetic-in-ggplot2) – tjebo Jul 11 '22 at 08:04

1 Answers1

2

Your requirement about "stable package" is somewhat vague, and I am surprised to hear that a CRAN package would not fulfil your requirement.

There is currently no vanilla ggplot2 way to achieve what you want and given the availability of the packages below, I don't think this is likely to come.

There are, as of today (December 2021), to my knowledge, "only" three packages available to allow creation of more than one scale for the same aesthetic. These are

I use the latter a lot and never had problems, so I think this would be a very good package for your simple use case.

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 1
    Thank you, a vanilla ggplot solution would have been the best, but this should work and thank you for writing this up in an answer – snaut Dec 07 '21 at 11:16