[R] R lattice bwplot: Fill boxplots with specific color depending on factor level

Pablo Fleurquin pablofleurquin at gmail.com
Wed Apr 22 18:03:24 CEST 2015


Hi,

I thoroughly looked for an answer to this problem with no luck.

I have a dataframe with 3 factor levels: YY, NN, YN

*>mydata <- rbind(data.frame(Col1 = rnorm(2*1000),Col2 =rep(c("A", "C"),
each=1000),Col3=factor(rep(c("YY","NN"), 1000))),data.frame(Col1 =
rnorm(1000),Col2 =rep(c("B")),Col3=factor(rep(c("YY","YN"), 500))))*

Being Col3 of factor type with 3 levels: NN YY YN

I want to make a boxplot using lattice bwplot and assign to each level a
specific color:






*# NN:>red=rgb(249/255, 21/255, 47/255)# YN:>amber=rgb(255/255, 126/255,
0/255)# YY:>green=rgb(39/255, 232/255, 51/255)*

Using bwplot function:


* >pl<-bwplot(mydata$Col1~mydata$Col3 |
mydata$Col2,data=mydata,ylab=expression(italic(R)),panel=function(...){panel.bwplot(...,groups=mydata$Col3,
fill=c(red,amber,green))})*

IF YOU REPRODUCE THE EXAMPLE YOU WILL SEE THAT THE COLORS ARE NOT RELATED
TO THE LEVELS IN MY DATAFRAME AS YY BOX IS NOT ALWAYS GREEN.

IS THERE A WAY TO ASSIGN YY:green, NN:red, YN:amber?

You can see the resulting figure in:
http://stackoverflow.com/questions/29802129/r-lattice-bwplot-fill-boxplots-with-specific-color-depending-on-factor-level

Thank you in advance!
Pablo

	[[alternative HTML version deleted]]



More information about the R-help mailing list