3

I'm attempting to overlay two plots with ggplot2. I don't have any problems doing it with scale_x_manual (as it's described in other question), but it crashes when I have to use scale_color_gradient...

My code is:

ggplot() + 
  geom_point(data = mtcars, aes(x = mpg, y = qsec, color = qsec)) +
  scale_color_gradient(low = "gray", high = "blue") +
  geom_point(data = mtcars, aes(x = drat, y = wt, color = wt)) +
  scale_color_gradient(low = "gray", high = "red")

But it prompts this message and only keeps one scale:

Scale for 'colour' is already present. 
Adding another scale for 'colour', which will replace the existing scale.

I've tried different options (scale_color_manual, scale_fill_gradient, scale_color_gradient2 or scale_color_gradientn...) but nothing seems to work. Please, any suggestions? Thanks for your help!

jgarces
  • 519
  • 5
  • 17
  • 1
    Just as the error message says, a plot can only have one color scale. Scales are not applied per layer, they apply to the whole plot. Notice how in that linked question, the same colors were used in both scales. – MrFlick Jun 14 '17 at 14:51
  • The trick is to use a different scale, as shown in the question you linked to, not two instances of the same scale. In your case, you could use a `color` scale for one variable on one points layer and a `fill` scale for another variable on a second points layer if you use a *fillable* shape like shape 21. – aosmith Jun 14 '17 at 14:51
  • Some workarounds here: [Superpose two layers of points with different colour aesthetics](https://groups.google.com/forum/#!topic/ggplot2/MEyMz1TMlvU) (including `fill` vs `color` and `scale_color_identity`). – Henrik Jun 14 '17 at 14:54
  • 2
    @MrFlick, thanks for marking it as duplicated, I didn't noticed of these (very usefull) other answer... at last! (thanks aosmith and Henrik for your responses also). – jgarces Jun 14 '17 at 15:20

0 Answers0