[R] Getting the values out of histogram (lattice)
Rolf Turner
rolf.turner at xtra.co.nz
Mon Sep 5 23:32:31 CEST 2011
On 05/09/11 21:26, Deepayan Sarkar wrote:
<SNIP>
> 1. The `official' way to get panel arguments is trellis.panelArgs(); e.g.,
>
>> p<- histogram(~rnorm(100) | gl(2, 50), type = "density")
>> str(trellis.panelArgs(p, 2))
> List of 5
> $ x : num [1:50] 0.277 1.144 1.13 -0.912 -0.892 ...
> $ breaks : num [1:9] -2.561 -1.979 -1.398 -0.816 -0.234 ...
> $ type : chr "density"
> $ equal.widths: logi TRUE
> $ nint : num 8
>
> 2. hist.constructor() is needed for technical reasons, and can be
> considered to be the same as hist() for this purpose. So the
> computations performed by panel.histogram() can be reduced to
>
>
> histogram.computations<-
> function(x, breaks, equal.widths = TRUE,
> type = "density", nint, ...)
> {
> if (is.null(breaks))
> {
> breaks<-
> if (is.factor(x)) seq_len(1 + nlevels(x)) - 0.5
> else if (equal.widths) do.breaks(range(x, finite = TRUE), nint)
> else quantile(x, 0:nint/nint, na.rm = TRUE)
> }
> hist(x, breaks = breaks, plot = FALSE)
> }
>
> which may be used as follows to get the ``actual data defining the histogram'':
>
>> a<- trellis.panelArgs(p, 2)
>> h<- do.call(histogram.computations, a)
>> str(h)
> List of 7
> $ breaks : num [1:9] -2.561 -1.979 -1.398 -0.816 -0.234 ...
> $ counts : int [1:8] 1 4 6 14 7 8 6 4
> $ intensities: num [1:8] 0.0344 0.1375 0.2062 0.4812 0.2406 ...
> $ density : num [1:8] 0.0344 0.1375 0.2062 0.4812 0.2406 ...
> $ mids : num [1:8] -2.2704 -1.6885 -1.1065 -0.5246 0.0573 ...
> $ xname : chr "x"
> $ equidist : logi TRUE
> - attr(*, "class")= chr "histogram"
As usual: Clear, concise, and useful! Thanks!
cheers,
Rolf
More information about the R-help
mailing list