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
Palette color order in bwplot (lattice violin plot) vs. boxplot
7 messages · Deepayan Sarkar, Luigi Ponti, Felix Andrews
2 days later
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
2 days later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100406/779887d0/attachment.pl>
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100407/bcea87ff/attachment.pl>
On 8 April 2010 03:34, Luigi Ponti <lponti at inbox.com> wrote:
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.
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100408/22f5cd7e/attachment.pl>