[R] Palette color order in bwplot (lattice violin plot) vs. boxplot

Luigi Ponti lponti at inbox.com
Thu Apr 1 13:10:47 CEST 2010


Hello,

I am trying to give different colors to boxes in a violin plot obtained 
via bwplot from lattice package using a color palette from RColorBrewer:

 > require(RColorBrewer)
 > MyPalette <- brewer.pal(6, "Set3")

A call to:

 > boxplot(count ~ spray, data = InsectSprays, col = MyPalette)

yields the example boxplot with each box colored according to the 
different colors from MyPalette. In addition, boxes are colored with the 
same color order of MyPalette. See

 > display.brewer.pal(6, "Set3")

However, when I do the same thing with a violin plot from the lattice 
package
 > require(lattice)
 > bwplot(count ~ spray, data = InsectSprays,
+        panel = function(..., box.ratio) {
+            panel.violin(..., col = "transparent",
+                         varwidth = FALSE, box.ratio = box.ratio)
+            panel.bwplot(..., fill = MyPalette, box.ratio = .1)
+        } )

boxplots are colored with the right colors (each box has a different 
color) but with a different color order -- too bad because I would like 
to color code the plot according to certain pre-defined colors. Same 
thing (wrong color order) with a simple bwplot:

 > bwplot(count ~ spray, data = InsectSprays, fill = MyPalette)

Is there a way to get the right color (i.e. same order as in MyPalette) 
in bwplot/panel.violin?

Kind regards,

Luigi



More information about the R-help mailing list