Carl Witthoft
Carl Witthoft

Reputation: 21532

Extract frequency values from lattice::histogram output

I'm repeating a defunct question which did not get any answers at the time. Unlike the base::hist function, lattice::histogram doesn't identify where, if at all, the y-axis data (aka frequency counts) are stored in the returned object. I did find a function in one of the attributes (list element) of the returned object which seems to suggest that the y-data are recalculated on the fly for plotting but not stored.

$yscale.components
function (lim, packet.number = 0, packet.list = NULL, right = TRUE, 
    ...) 
{
    comps <- calculateAxisComponents(lim, packet.list = packet.list, 
        packet.number = packet.number, ...)
    list(num.limit = comps$num.limit, left = list(ticks = list(at = comps$at, 
        tck = 1), labels = list(at = comps$at, labels = comps$labels, 
        cex = 1, check.overlap = comps$check.overlap)), right = right)
}
<bytecode: 0x00000294e6762dc0>
<environment: namespace:lattice>

I did locate the input dataset, contained in $panel.args .

Upvotes: 1

Views: 70

Answers (1)

Quinten
Quinten

Reputation: 41437

The values are stored in what you said right p$panel.args[[1]]$x and the breaks are in p$panel.args.common$breaks. You can use cut and table to determine the frequency like this:

library(lattice)
p <- lattice::histogram(~ mpg, mtcars)
p

values <- p$panel.args[[1]]$x
values
#>  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
#> [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
#> [31] 15.0 21.4
breaks <- p$panel.args.common$breaks
breaks
#> [1]  9.46 13.69 17.92 22.15 26.38 30.61 34.84

table(cut(values, breaks))
#> 
#> (9.46,13.7] (13.7,17.9] (17.9,22.1] (22.1,26.4] (26.4,30.6] (30.6,34.8] 
#>           3          10          10           4           3           2

Created on 2022-08-25 with reprex v2.0.2

Upvotes: 2

Related Questions