Gerrit Eichner
2013-Dec-09 22:17 UTC
[R] lattice: superposed boxplots with same colors for rectanglesand umbrellas and filled boxes
Dear R-list, I've been trying to produce a sort of an interaction plot wherein colored stripplots and boxplots are superposed. In particular, I want the colors of the (transparently) filled boxes to be the same as the colors of the box borders (rectangle) and their whiskers (umbrella). Below I'm going to create an artificial data set and provide the code with which I've come up so far, and which is a fusion and adaptation of a few pieces of code I've found in the help pages and the mail archives. It does almost what I want, but still colors the rectangles and the umbrellas in black (of course, because it is the setting in the example presented below). However, the latter is what I want to change. x <- c( rep( 1:4, each = 10), rep( -2, 40)) Data <- data.frame( F1 = gl( 4, 10, length = 80, labels = LETTERS[ 1:4]), F2 = gl( 2, 40), Y = x + rnorm( length( x))) mycolors <- c( "red", "blue") myalpha <- 0.33 bwplot( Y ~ F1, groups = F2, data = Data, col = mycolors, fill = mycolors, jitter.data = TRUE, panel = panel.superpose, panel.groups = function(..., pch, col, alpha){ panel.stripplot(...) panel.bwplot(..., do.out = FALSE) }, par.settings = list( box.dot = list( alpha = myalpha), box.rectangle = list( col = "black", alpha = myalpha), box.umbrella = list( col = "black", alpha = myalpha)) ) If I'd provide mycolors instead of "black" to the col-component of the (sub-)lists given to par.settings, the coloring of the respective components of the boxplots would not be what I want. Having studied the code of panel.superpose() and panel.bwplot() it appears to me that panel.bwplot() cannot access the settings created by panel.supperpose when it comes to drawing the rectangle and umbrella of a boxplot. It only accesses box.[dot, rectangle, umbrella], which does not give what I need. I may have overlooked the obvious and would be quite grateful if somebody could give me a hint where to look further. Or is a workaround necessary (and available)? Thanks a lot in advance! Best Regards -- Gerrit --------------------------------------------------------------------- Dr. Gerrit Eichner Mathematical Institute, Room 212 gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/cms/eichner
Richard M. Heiberger
2013-Dec-09 22:52 UTC
[R] lattice: superposed boxplots with same colors for rectanglesand umbrellas and filled boxes
Gerrit, Thank you for the opportunity to illustrate this. I solved this problem last week and it will be included in the next version of the HH package (about a month away). panel.bwplot.constantColor <- function(..., col, fill, cex, pch) { ## to be included in next version of the HH package box.save <- list(box.dot=trellis.par.get("box.dot"), box.rectangle=trellis.par.get("box.rectangle"), box.umbrella=trellis.par.get("box.umbrella"), plot.symbol=trellis.par.get("plot.symbol")) trellis.par.set(box.dot=list(col=col), box.rectangle=list(col=col), box.umbrella=list(col=col), plot.symbol=list(col=col)) panel.bwplot(..., fill=col, cex=cex, pch=pch) trellis.par.set(box.save) } bwplot( Y ~ F1, groups = F2, data = Data, col = mycolors, fill = mycolors, jitter.data = TRUE, pch=19, panel = panel.superpose, panel.groups = function(..., pch, col, alpha){ panel.stripplot(...) panel.bwplot.constantColor(..., pch=pch, col=col, alpha=alpha, do.out = FALSE) }, par.settings = list( box.dot = list( alpha = myalpha), box.rectangle = list( col = mycolors, alpha = myalpha), box.umbrella = list( col = mycolors, alpha = myalpha)) ) Rich On Mon, Dec 9, 2013 at 5:17 PM, Gerrit Eichner <Gerrit.Eichner at math.uni-giessen.de> wrote:> Dear R-list, > > I've been trying to produce a sort of an interaction plot wherein colored > stripplots and boxplots are superposed. In particular, I want the colors of > the (transparently) filled boxes to be the same as the colors of the box > borders (rectangle) and their whiskers (umbrella). Below I'm going to create > an artificial data set and provide the code with which I've come up so far, > and which is a fusion and adaptation of a few pieces of code I've found in > the help pages and the mail archives. It does almost what I want, but still > colors the rectangles and the umbrellas in black (of course, because it is > the setting in the example presented below). However, the latter is what I > want to change. > > x <- c( rep( 1:4, each = 10), rep( -2, 40)) > Data <- data.frame( F1 = gl( 4, 10, length = 80, labels = LETTERS[ 1:4]), > F2 = gl( 2, 40), Y = x + rnorm( length( x))) > > mycolors <- c( "red", "blue") > myalpha <- 0.33 > bwplot( Y ~ F1, groups = F2, data = Data, > col = mycolors, fill = mycolors, jitter.data = TRUE, > panel = panel.superpose, > panel.groups = function(..., pch, col, alpha){ > panel.stripplot(...) > panel.bwplot(..., do.out = FALSE) > }, > par.settings = list( box.dot = list( alpha = myalpha), > box.rectangle = list( col = "black", > alpha = myalpha), > box.umbrella = list( col = "black", > alpha = myalpha)) > ) > > > If I'd provide mycolors instead of "black" to the col-component of the > (sub-)lists given to par.settings, the coloring of the respective components > of the boxplots would not be what I want. Having studied the code of > panel.superpose() and panel.bwplot() it appears to me that panel.bwplot() > cannot access the settings created by panel.supperpose when it comes to > drawing the rectangle and umbrella of a boxplot. It only accesses box.[dot, > rectangle, umbrella], which does not give what I need. > > I may have overlooked the obvious and would be quite grateful if somebody > could give me a hint where to look further. Or is a workaround necessary > (and available)? > > Thanks a lot in advance! > > Best Regards -- Gerrit > > --------------------------------------------------------------------- > Dr. Gerrit Eichner Mathematical Institute, Room 212 > gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen > Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany > Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/cms/eichner > > ______________________________________________ > R-help at r-project.org 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.