3

I have what seems to be a very basic problem, but I cannot solve it, as I have barely used ggplots2... I just want that the plot on the left uses the colors in the variable color1 and the plot on the right uses the colors in the variable color2. This is a MWE:

library(reshape2)
library(ggplot2)

a.df <- data.frame(
  id=c("a","b","c","d","e","f","g","h"), 
  var1=c(1,2,3,4,5,6,7,8), var2=c(21,22,23,24,25,26,27,28), 
  var3=c(56,57,58,59,60,61,62,63), 
  color1=c(1,2,"NONE","NONE",1,2,2,1), 
  color2=c(1,"NONE",1,1,2,2,"NONE",2)
)

a.dfm <- melt(a.df, measure.vars=c("var2","var3"))

ggplot(a.dfm, aes(x=value, y=var1, color=color1)) + 
  geom_point(shape=1) + 
  facet_grid(. ~ variable)

Thanks a lot!

Andrie
  • 176,377
  • 47
  • 447
  • 496
DaniCee
  • 2,397
  • 6
  • 36
  • 59
  • 1
    I have a feeling that your data needs to be re-arranged further before the plotting. But to tell if that is the case, can you please provide some more context, and describe your variables a little bit more. Cheers. – Henrik Oct 03 '13 at 20:42
  • Not an exact duplicate, but I think [this question](http://stackoverflow.com/questions/10267583/programmatically-specifying-colours-in-scale-fill-manual-ggplot-call) covers a lot of the same ground and is worth checking out. – SlowLearner Oct 03 '13 at 21:44
  • this is just a very simple example; I have a bigger data frame with a similar structure, with 3 different measuring variables. I want to plot the 1st (in the y axis) versus the 2nd and the 3rd independently (in the x axis). Besides I isolate entries which are the 25% with higher var1 values and higher var2 values, the 25% with lower var1 values and lower var2 values, and the same for var1 vs var3. These are the 2 color scales I want to depict (left=top 25% var1vs2 in red, bottom 25% var1vs2 in blue, rest in black; right=top 25% var1vs3 in red, bottom 25% var1vs3 in blue, rest in black). – DaniCee Oct 03 '13 at 21:46
  • I like simple examples! Thanks for the additional information. There are so many nice ways to map colours in `ggplot` and `stat` functions, so it is good to get a better feeling of how your variables are derived, in order to determine what needs to be calculated in beforehand, and what is more convenient to let `ggplot` take care of. – Henrik Oct 03 '13 at 21:50

1 Answers1

4

I think the easiest approach with your data is to create an additional column which has the color defined appropriately based on the value of variable. Since there are just two possible values that variable can take on, this isn't that hard.

a.dfm2 <- transform(a.dfm, 
                    color.use = ifelse(variable=="var2", 
                                       as.character(color1), 
                                       as.character(color2)))

ggplot(a.dfm2, aes(x=value, y=var1, color=color.use)) + 
  geom_point(shape=1) + 
  facet_grid(. ~ variable)

enter image description here

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
  • 1
    Works like a charm! I'm not sure I understand the transform() step, but I'll work on it! Thanks! – DaniCee Oct 04 '13 at 15:03