Monday 15 April 2013

r - ggplot2 barchart: how to set fill colours manually if zero counts are present -



r - ggplot2 barchart: how to set fill colours manually if zero counts are present -

i want create barcharts manual fill colors using ggplot2.

library(ggplot2)

this works fine if there no 0 counts in of categories.

d <- data.frame(x=factor(1:7, levels=1:7)) g <- ggplot(d, aes(x=x)) + geom_bar(fill=rep(c("red", "blue"), length=7)) + scale_x_discrete(drop=false) g

but if of counts 0 error:

d2 <- data.frame(x=factor(1:6, levels=1:7)) g %+% d2 error: incompatible lengths set aesthetics: fill

what can it?

you need alter way you're doing fill:

d2 <- data.frame(x=factor(1:6, levels=1:7)) g <- ggplot(d2, aes(x=x)) + geom_bar(aes(fill=x)) + scale_x_discrete(drop=false) + scale_fill_manual(values=rep(c("red", "blue"), length=7)) g

r ggplot2

No comments:

Post a Comment