Ed G
Ed G

Reputation: 822

ggplots conditional colour scale

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

Upvotes: 4

Views: 4364

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98509

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"))

Upvotes: 5

Related Questions