[R] Multiple color schemes for barchart (lattice)

hadley wickham h.wickham at gmail.com
Wed Jun 6 22:07:24 CEST 2007


On 6/6/07, Sarah Hawley <sarah at canaryfoundation.org> wrote:
> Hello R-help.
>
> I am trying to make a stacked barplot where the color of the sections of
> each bar depend on another variable.
>
> > myData[1:11,]
>    score   percent    marker     cellType Malignant
> 1      0 100.00000 ESR1 (ER) Bladder.M(5) TRUE
> 2      0  80.00000      PAX8 Bladder.M(5) TRUE
> 3      1  20.00000      PAX8 Bladder.M(5) TRUE
> 4      0 100.00000 ESR1 (ER)   Brain.N(3) FALSE
> 5      0 100.00000      PAX8   Brain.N(3) FALSE
> 6      3 100.00000 ESR1 (ER) Breast.M(11) TRUE
> 7      0 100.00000      PAX8 Breast.M(11) TRUE
> 8      0  36.36364 ESR1 (ER) Cervix.M(11) TRUE
> 9      1   9.09091 ESR1 (ER) Cervix.M(11) TRUE
> 10     2  18.18182 ESR1 (ER) Cervix.M(11) TRUE
> 11     3  36.36364 ESR1 (ER) Cervix.M(11) TRUE
>
> palette <- palette(gray(seq(0, 1,len=4)))
> trellis.par.set(list(par.xlab.text=list(cex=0.85)
>                    , superpose.polygon=list(col=palette())
>                    , axis.text=list(cex=0.8)))
>
>
> barchart(percent~cellType|marker
>         , groups=score
>         , data=myData
>         , stack=TRUE
>         , xlab='N=Normal/Benign, M=Malignant'
>         , ylab='Percentage of Cores Staining'
>         , color=palette()
>         , auto.key = list(points = FALSE, rectangles = TRUE, space = "top")
>         , scales=list(x=list(rot=70))
>         , layout=c(1,2))
>
> I would like to make the color scheme of the bar differ according to the
> variable 'Malignant' and add a second color scheme to the key.

It's pretty easy to do this with ggplot2 - see
http://had.co.nz/ggplot2/position_stack.html for some examples.

Hadley



More information about the R-help mailing list