1

When I plot the following:

library(ggplot2)

ggplot() +
  geom_point(data=mtcars, mapping=aes(x=mpg, y=wt, col=factor(cyl)))

I get this plot: enter image description here

Now I want to add different data with different groups and colors

library(ggplot2)

data = data.frame(x = rep(12, times=50), y = seq(1, 5, length.out = 50), c = c(rep(1, times=10),
                                                                               rep(2, times=10),
                                                                               rep(3, times=10),
                                                                               rep(4, times=10),
                                                                               rep(5, times=10)))

ggplot() +
  geom_point(data=mtcars, mapping=aes(x=mpg, y=wt, col=factor(cyl))) +
  geom_point(data=data, aes(x=x, y=y, col=factor(c)), inherit.aes = FALSE)

But I get this plot where the colors of the first layer changed. How can I color the second layer after the c variable, without changing the first layer? enter image description here

Benni
  • 795
  • 2
  • 7
  • 20
  • 1
    I don't find 'the best' answer right now, but google "consistent color between plots ggplot" and you get the idea. – Henrik Nov 21 '18 at 18:42

2 Answers2

2

You cannot map a single aesthetic (in this case, color) to two different sets of values in the same plot. ggplot is interpreting your commands in the only valid way: constructing a single color scale that includes all the unique values in mtcars$cyl and data$c.

We could, however, get close to what you want by mapping color to one set of values and fill to another. We can use pch = 21, since this point style has both a color and a fill. We turn off the point stroke (color) in one layer and the point fill in the other:

ggplot() +
  geom_point(data=mtcars, mapping=aes(x=mpg, y=wt, col=factor(cyl)), pch = 21) +
  geom_point(data=data, aes(x=x, y=y, fill=factor(c)), pch = 21, color = '#00000000')

enter image description here

jdobres
  • 11,339
  • 1
  • 17
  • 37
1

However, we have options with plot() of base R. According to this answer we can define color palettes fitting for our data:

rbPal1 <- colorRampPalette(c('red','blue'))
rbPal2 <- colorRampPalette(c('green', 'purple','orange'))
mtcars$cyl.col <- rbPal1(8)[mtcars$cyl]  # for mtcars
data$c.col <- rbPal2(5)[data$c]  # for your data

Then we can plot it by referencing to those colors, even with a legend.

with(mtcars, plot(mpg, wt, col=cyl.col, pch=16))
with(data, points(y ~ x, col=c.col, pch=16))
legend("topright", as.character(unique(mtcars$cyl)), title="cyl",
       col=unique(mtcars$cyl.col), lty=0, lwd = 2, pch=16)
legend("bottomright", as.character(unique(data$c)), title="c",
        col=unique(data$c.col), lty=0, lwd = 2, pch=16)

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thank you, that's a valid solution. Sadly I can only accept one answer. – Benni Nov 22 '18 at 10:17
  • @Benni No worries, you explicitly asked for ggplot, I just wanted to show, that in R plotting isn't just `ggplot()`. – jay.sf Nov 22 '18 at 11:28