Skip to content

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

7 messages · Deepayan Sarkar, Luigi Ponti, Felix Andrews

#
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
2 days later
#
On Thu, Apr 1, 2010 at 4:10 AM, Luigi Ponti <lponti at inbox.com> wrote:
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
2 days later
#
On 6 April 2010 20:14, Luigi Ponti <lponti at inbox.com> wrote:
Your previous 'panel' function could now be passed as 'panel.groups'
(see ?panel.superpose); alternatively, if you have a simple panel
function you can pass arguments to it directly through the main call
to bwplot().
help("density") says:
    by default, the values of 'from' and 'to' are 'cut' bandwidths
beyond the extremes of the data.

and the default value of 'cut' is 3.
Therefore if you want to limit the density to the data range, pass cut = 0.



-- 
Felix Andrews / ???
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
#
On 8 April 2010 03:34, Luigi Ponti <lponti at inbox.com> wrote:

            
The problem is that panel.superpose() passes on arguments 'col.line'
and 'col.symbol', but unless otherwise specified, 'col' is passed as
NA (which is an invisible color). Your options are to specify pch =
"|", which is not affected by 'col', or to pass 'col' explicitly:

bwplot(count ~ spray, data = InsectSprays,
       groups = spray,
       panel = panel.superpose,
       panel.groups =  function(..., box.ratio, col) {
            panel.violin(..., col = col,
                         varwidth = FALSE, box.ratio = box.ratio, cut = 0)
            panel.bwplot(..., col = "black", box.ratio = .1)
        },
        fill = MyPalette, pch = 16
)




-- 
Felix Andrews / ???
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C