4

I'm working on some density plots where the fill is conditional on a value (0.05)

library(plyr)
library(ggplot2)

allNorm<-data.frame(value=rnorm(300),
                 aCategory=rep(1:3, c(100, 100, 100)))
allNorm<-ddply(allNorm, .(aCategory), mutate,
            shapWilkP=shapiro.test(value)$p.value)

mixed<-data.frame(value=c(rlnorm(200), rnorm(100)),
                aCategory=rep(1:3, c(100, 100, 100)))
mixed<-ddply(mixed, .(aCategory), mutate,
            shapWilkP=shapiro.test(value)$p.value)

ggplot(allNorm, (aes(x=value)))+
  geom_density(aes(fill=shapWilkP>0.05))+
  facet_wrap(~aCategory)

ggplot(mixed, (aes(x=value)))+
  geom_density(aes(fill=shapWilkP>0.05))+
  facet_wrap(~aCategory)

This works (almost) - I just need to work out how to force TRUE to be blue all the time.

Could anybody help?

Thanks

Ed G
  • 802
  • 1
  • 6
  • 19

1 Answers1

5

Add to both plots scale_fill_manual() where you set desired colors for TRUE and FALSE values.

+ scale_fill_manual(values=c("FALSE"="red","TRUE"="blue"))
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • Fantastic - I'd got as far as scale_fill_manual(values=c("red", "blue")). Didn't know you could add in the TRUE and FALSE. Thank you very much. – Ed G Mar 14 '13 at 18:30