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

Felix Andrews felix at nfrac.org
Wed Apr 7 02:10:46 CEST 2010


On 6 April 2010 20:14, Luigi Ponti <lponti at inbox.com> wrote:
> On 04/04/2010 06:21, Deepayan Sarkar wrote:
>> 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:
>>> [...]
>>> 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).
>>
>
> Thanks! This way I get violin plots colored in the correct order.
> However, I lose control on panel.violin -- not sure if/how I can regain
> that (i.e., the stuff the was done by the panel = function(){} above).

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().


> Also, I have noticed that violin plots extend beyond the data range: is
> this the default behavior? In ?panel.violin and ?density, I read that
> default parameters should allow "the estimated density to drop to
> approximately zero at the extremes". For example, the minimum value of
> the "count" variable (data = InsectSprays) is zero, but the violin plots
> do extend below zero. Am I missing something?

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
-- 
http://www.neurofractal.org/felix/



More information about the R-help mailing list