1

I have two datasets which I plot one above the other. I want to use different manual color ramps for them. However, scale_color_manual changes color for both data sets at the same time.

set.seed(123)
data1 <- data.frame(x=rnorm(25,2,.5),y=rnorm(25,2,1),z=factor(sample(c(1:3),25,replace=TRUE)))
data2 <- data.frame(x=rnorm(25,4,1),y=rnorm(25,2,.5),z=factor(sample(c(1:3),25,replace=TRUE)))

col1 <- c("lightblue","blue","darkblue") #corresponding to levels of 1...3 in data1
col2 <- c("lightgreen","green","darkgreen") #corresponding to levels of 1...3 in data2

ggplot(mapping=aes(x=x,y=y,color=z,size=5)) +
  geom_point(data=data1) +
  scale_color_manual(values=col1)+
  geom_point(data=data2) +
  scale_color_manual(values=col2)

Actual results area all in green. However, points more to the left should be in blue.

Contemplavit
  • 115
  • 1
  • 1
  • 10

1 Answers1

1

ggplot doesn't have an inbuilt option for this but you can prepare your data to achieve it:

data1$color1 <- col1[data1$z]
data2$color2 <- col2[data2$z]

ggplot()  +
  geom_point(data=data1, aes(x=x,y=y,color=color1), size=5) +
  geom_point(data=data2, aes(x=x,y=y,color=color2), size=5) +
  scale_color_identity() 

enter image description here

Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34