[R] Stacked barchart color

hadley wickham h.wickham at gmail.com
Sat Jun 16 08:38:25 CEST 2007


On 6/16/07, owenman <solberg at speakeasy.net> wrote:
>
> 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

Hi Owen,

The identity scale won't create a legend, unless you tell it what
labels it should use - there's an example at
http://had.co.nz/ggplot2/scale_identity.html.  Otherwise, if you have
a continuous scale and you want something that works in black and
white, p + scale_fill_gradient(low="white", high="black") might be
easier.

Hadley


>
> > y$color = factor(y$Fnd)
> > y$color = c("black","darkgray","lightgray","white")
> > y
>       Fnd 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()
> > p
>
>
>
>
> hadley 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.
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list