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"))
On 6/12/07, Dieter Menne <dieter.menne at menne-biomed.de> 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"))The easiest way is to create a new factor with suitable levels: replace groups=year by groups=year:variety or the more verbose groups=interaction(year, variety) -Deepayan
On 6/12/07, Dieter Menne <dieter.menne at menne-biomed.de> 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 (http://had.co.nz/ggplot2) as follows: library(ggplot2) 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 http://had.co.nz/ggplot2/scale_identity.html and http://had.co.nz/ggplot2/position_stack.html for more details. Hadley
Hi Hadley, I tried your suggestion, using ggplot2, but I am still having a problem. The final plot lacks the figure legend -- which it had before I added the scale_fill_identity() bit. Can you see what I am doing wrong? (By the way, all I am trying to do is make the figure monochrome friendly. Is there an easy way to prepare ggplot graphics for a monochrom device?) Thanks,Owen> y$color = factor(y$Fnd) > y$color = c("black","darkgray","lightgray","white") > yFnd locus Freq color 1 signeg A 0.087248322 black 2 neg A 0.711409396 darkgray 3 pos A 0.201342282 lightgray 4 sigpos A 0.000000000 white 5 signeg C 0.320754717 black 6 neg C 0.603773585 darkgray 7 pos C 0.075471698 lightgray 8 sigpos C 0.000000000 white 9 signeg B 0.157534247 black 10 neg B 0.732876712 darkgray 11 pos B 0.109589041 lightgray 12 sigpos B 0.000000000 white> p = ggplot(y, aes(x=locus, y=Freq, fill=color)) + > geom_bar(position="fill") + scale_fill_identity() > phadley wrote:> > > Hi Dieter, > > You can do this with ggplot2 (http://had.co.nz/ggplot2) as follows: > > library(ggplot2) > > 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 http://had.co.nz/ggplot2/scale_identity.html and > http://had.co.nz/ggplot2/position_stack.html for more details. > > Hadley > >-- View this message in context: http://www.nabble.com/Stacked-barchart-color-tf3909162.html#a11149419 Sent from the R help mailing list archive at Nabble.com.