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

Pablo Fleurquin pablofleurquin at gmail.com
Thu Apr 23 10:08:28 CEST 2015


Thank you both.

I just wanted to point out that before assigning the order of colors in
vector col, one should check that it corresponds with how levels are
ordered in levels(mydata$Col3).

Best,
Pablo

2015-04-23 5:46 GMT+02:00 Richard M. Heiberger <rmh at temple.edu>:

> Pablo,
>
> I would do it similarly.  I would also place the box and whiskers in
> the specified colors.
>
> ## install.packages(HH)  ## if you don't have it
> library(HH)
>
> bwplot(mydata$Col1~mydata$Col3 | mydata$Col2,data=mydata,
>        groups = Col3,
>        as.table = TRUE, # added to make it easier for factor levels
>        layout = c(3,1), # looks nicer and easier to read
>        panel = panel.bwplot.superpose,
>        col = c(red,"darkorange",green),
>        fill = c(red,"darkorange",green), fill.alpha=.6)
>
> I changed your amber to "darkorange" as the amber lines are almost
> invisible.
>
> Rich
>
> On Wed, Apr 22, 2015 at 9:26 PM, Duncan Mackay <dulcalma at bigpond.com>
> wrote:
> > hi Pablo
> >
> > set.seed(1) # for reproducibility of data.frame
> > 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))))
> > mydata$Col2 <- factor(mydata$Col2)
> >
> > In future please do not put * at end makes it harder to copy
> >
> > red=rgb(249/255, 21/255, 47/255)
> > amber=rgb(255/255, 200/255,0/255) # amended to reveal a colour difference
> > green=rgb(39/255, 232/255, 51/255)
> >
> > # As  Deepayan Sarkar said bwplot is different to others
> >
> > bwplot(mydata$Col1~mydata$Col3 | mydata$Col2,data=mydata,
> >        groups = Col3,
> >        as.table = TRUE, # added to make it easier for factor levels
> >        layout = c(3,1), # looks nicer and easier to read
> >        panel = panel.superpose,
> >        panel.groups = panel.bwplot,
> >        fill = c(red,amber,green)
> > )
> >
> > Duncan
> >
> > Duncan Mackay
> > Department of Agronomy and Soil Science
> > University of New England
> > Armidale NSW 2351
> > Email: home: mackay at northnet.com.au
> >
> > -----Original Message-----
> > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Pablo
> > Fleurquin
> > Sent: Thursday, 23 April 2015 02:03
> > To: r-help at r-project.org
> > Subject: [R] R lattice bwplot: Fill boxplots with specific color
> depending
> > on factor level
> >
> > 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-w
> > ith-specific-color-depending-on-factor-level
> >
> > Thank you in advance!
> > Pablo
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list