[R] densityplot and histogram
Deepayan Sarkar
deepayan at stat.wisc.edu
Wed Oct 20 22:12:00 CEST 2004
On Tuesday 19 October 2004 17:12, Jean Eid wrote:
> Is there any function like par(new=T) for lattice. I want to plot a
> histogram in percentages on the right hand side and also superimpose the
> densityplot with its density scale on the lhs. so far I am only able to do
> this
> histogram( temp[,2]~ temp[,1],nint=100,type="desnity",
> xlab = "Population Size",
> panel = function(x, ...) {
> panel.histogram(x, ...)
> panel.densityplot(x, col = "red", plot.points=F, lwd=2)
> } )
>
> If I change type="density" to type="percent" the scales for the
> densityplot will be too low and all I see is a horizontal line at zero
> (this is as expected) . However, I tried par(new=T) and nothing happens. I
> want to be able to put percenstages on axis 2 and density values at axis
> 4.
I don't think par(new=T) is what you should be looking for (and incidentally,
par settings have no effect on lattice plots). It's possible to add axes
after the plot (easier than it was before 2.0.0), but the design of lattice
doesn't allow you to easily allocate enough space for the second set of axes.
You can still do it, but it would be kludgy.
Here's an example (you need to know what 'mult' should be -- it's the factor
that converts the density scale to the percent scale -- it would depend on
the widths of the bins and the length of x):
x <- rnorm(200)
mult <- 60 ## meaningless in this case
histogram(x, type = "percent",
panel = function(x, ...) {
panel.histogram(x, ...)
d <- density(x)
panel.lines(d$x, mult * d$y, col = 'red')
},
scales = list(y = list(tck = c(1, 0))),
par.settings = list(layout.widths = list(right.padding = 5)))
trellis.focus("panel", 1, 1, clip.off = TRUE)
at <- pretty(c(0, 25)/mult)
panel.axis(side = "right",
at = mult * at, labels = at,
outside = TRUE)
trellis.unfocus()
More information about the R-help
mailing list