Re: [R] Stacked barchart color

From: hadley wickham <>
Date: Tue, 12 Jun 2007 20:31:47 +0200

On 6/12/07, Dieter Menne <> wrote:
> Dear Latticer,
> I want to give individual colors to all elements in a simple stacked
> barchart. I know why the example below does not work (and it is a excellent
> default), but is there any workaround for this?
> Dieter
> # This only colors red and green, but I want blue and gray for Peatland.
> barchart(yield ~ variety , groups=year, data = barley, stack = TRUE,
> subset=site=="Grand Rapids" & variety %in% c("Velvet","Peatland"),
> col=c("red","green","blue","gray"))

Hi Dieter,

You can do this with ggplot2 ( as follows:


barley1 <- subset(barley, site=="Grand Rapids" & variety %in% c("Velvet","Peatland"))
barley1[] <- lapply(barley1, "[", drop=TRUE)

qplot(variety, yield, data=barley1, geom="bar", stat="identity", fill=factor(year))

barley1$fill <- c("red","green","blue","gray") qplot(variety, yield, data=barley1, geom="bar", stat="identity", fill=fill) + scale_fill_identity()

See and for more details.

Hadley mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 12 Jun 2007 - 18:48:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 16 Jun 2007 - 00:31:58 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.