[R] Lattice: Superimposing histograms with different colors and transparency effects
Yvonnick NOEL
yvonnick.noel at uhb.fr
Fri Aug 13 22:12:22 CEST 2010
Dear users,
I would like to plot several histograms superimposed on the same panel
with different colors, with superimposed polygons appearing with
transparency effects. I also want estimated densities to appear on the
same plot. For several reasons, including that I like it, I want to use
the lattice package.
I have several questions regarding the use of the 'histogram' function
with a group structure.
I first thought that defining well-chosen values of alpha in trellis
parameters would give the transparency effets, but this is not the case:
library(lattice)
# Some data
x = c(rnorm(100), rnorm(100,2), rnorm(100,4))
grouping = gl(3,100,300)
# Trellis parameters
trellis.par.set("superpose.polygon"=list(alpha=rep(.5,3)))
histogram(~x ,groups=grouping, type = "density",
panel = "panel.superpose",
panel.groups = function(x,...) {
panel.histogram(x,...)
panel.mathdensity(dmath=dnorm,args =
list(mean=mean(x),sd=sd(x)),...)
})
Besides transparency, I get no filling colors at all in the, even though
plot.polygon and superpose.polygon parameters are set. I clearly need to
define my own colors with alpha channel set:
mycolors = rgb(c(228, 55, 77),
c(26, 126, 175),
c(28, 184, 74),alpha = 50,maxColorValue = 255)
... and include 'mycolors' as an explicit argument in the histogram
function:
histogram(~x ,groups=grouping, type = "density",ylim=c(0,.45),
panel = "panel.superpose",col=mycolors,
auto.key=list(space="right",rectangles=FALSE,col=mycolors),
panel.groups = function(x,...) {
panel.histogram(x,...)
panel.mathdensity(dmath=dnorm,
args=list(mean=mean(x),sd=sd(x)),...)
})
-> First question: Is it the only mean to get histogram bars filled, or
do I do something wrong in the use of trellis.arg.set ?
The problem with the previous approach is that the 'col' argument also
affects the density curves colors, for which I don't want transparency
effects. The 'col.lines' argument doesn't seem to change anything.
Removing the (...) arguments is not an interesting option, as it
suppresses some useful parameters for histograms (breaks, etc.).
-> Second question: How do I get superimposed density curves with colors
that differ from the bar colors (i.e. here: No transparency effects)?
-> Third question: How do I find nice (and common) ylim values for the
three histograms? I have set ylim=c(0,.45) above by hand, but I would
like to see this calibrated beforehand. Adding a prepanel function is
probably the way to go, but I am not sure how to manage this.
-> Fourth question: I would like the bar borders to have colors that
also vary from group to group, but unlike the 'col=' argument, adding a
'border=mycolors' argument in the histogram function call change colors
from bar to bar!
Thank you very much in advance.
Best wishes,
Yvonnick Noel, PhD.
University of Brittany, Rennes
France
More information about the R-help
mailing list