Luigi Ponti
2010-Apr-01 11:10 UTC
[R] Palette color order in bwplot (lattice violin plot) vs. boxplot
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
Deepayan Sarkar
2010-Apr-04 04:21 UTC
[R] Palette color order in bwplot (lattice violin plot) vs. boxplot
On Thu, Apr 1, 2010 at 4:10 AM, Luigi Ponti <lponti at inbox.com> wrote:> 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?The correct approach would be along the lines of bwplot(count ~ spray, data = InsectSprays, groups = spray, panel = panel.superpose, panel.groups = panel.violin, col = MyPalette) (unlike panel.xyplot etc., panel.bwplot does not explicitly handle grouping). -Deepayan