Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Values from Lattice Histogram

Tags:

r

lattice

I have created an equal area histogram using histogram() in the Lattice, however, I can't seem to find a way to extract the values in the 20 equal are sized bins. Any suggestions? I've also looked into using ggplot2, but I can't create equal area sized bins while having the capability of extracting the data using ggplot_build. Here's an example:

library(lattice)

cell <- 1:20

psi <- rnorm(1:20)

histogram(df$psi,
          freq = TRUE,
          equal.width = FALSE,
          breaks = NULL,
          nint = 20)

Equal Area Histogram

As you can see in the figure, an equal area histogram is generated, but now I'd like to know which values from df$psi are within each bin of the histogram. I've tried assigning the plot to a new variable dfhist <- hist() and then inspecting the new variable dfhist$ yet it doesn't appear as though the results for each bin are stored anywhere. Thanks for your time.

like image 425
Matt Avatar asked Mar 13 '26 11:03

Matt


1 Answers

The values are in the panel.args[[1]]$x of your plot called for example p like this:

library(lattice)
cell <- 1:20
psi <- rnorm(1:20)
df <- data.frame(cell = cell,
                 psi = psi)

p <- lattice::histogram(df$psi,
          freq = TRUE,
          equal.width = FALSE,
          breaks = NULL,
          nint = 20)

p$panel.args[[1]]$x
#>  [1]  0.57126799  0.52931270  1.09610583  0.40210029 -0.51261307 -1.47656641
#>  [7] -1.15286025 -1.62896968 -0.41758147  0.46671218  2.54717967 -1.08131256
#> [13] -0.03050443 -0.25765653  1.60843681  1.43767274  1.72095174 -0.21045430
#> [19]  0.04033122 -0.38105514

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

like image 181
Quinten Avatar answered Mar 15 '26 01:03

Quinten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!