On 07/04/2010 02:10, Felix Andrews wrote:
> On 6 April 2010 20:14, Luigi Ponti <lponti@inbox.com> wrote:
>   
>> On 04/04/2010 06:21, Deepayan Sarkar wrote:
>>     
>>> On Thu, Apr 1, 2010 at 4:10 AM, Luigi Ponti <lponti@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().
>   

Thanks for the hint, Felix: the following code makes it but (don't know 
why) the median dots disappear.

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

I tried to change box.dot parameter to no avail. Not sure what is going 
on here. It would appear that the panel.groups = function(){} is not 
sure where to read graphical parameters from (just guessing). It would 
be interesting to know your opinion.

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

Sorry, I misinterpreted the text -- my fault. I set cut = 0 (see code 
above) and now it works as expected.

Thanks again and regards,

Luigi



	[[alternative HTML version deleted]]

