using histogram to find the break associated with the top x% of data points

I have a simple histogram of the frequency of gene expression values for a dataset of 100 datapoints.

I want to find the expression value (x axis) that corresponds to the top 20% of the data points. Or in this case, I want the break number that is associated with datapoint 80 when organized in the histogram

Ive been doing this manually by adding the breaks starting with the last break until I hit reach a count of 20, and then looking to see which break this is in. Sometimes because of the breaks my count is not exactly 20 but thats fine.

Does this make sense? Is there an easy way to ask this hist function this question? Even if it was as simple as "Which break has #80"

Thanks!

There's a function for that! classInf::classIntervasl()

library(classInt)
if (!require("spData", quietly=TRUE)) {
  message("spData package needed for examples")
  run <- FALSE
} else {
  run <- TRUE
}
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="quantile"))
}
#> style: quantile
#>   one of 3,921,225 possible partitions of this variable into 5 classes
#>  [15.57,33.822) [33.822,50.114) [50.114,57.454) [57.454,73.368)  [73.368,155.3] 
#>              21              20              20              20              21

Created on 2020-04-02 by the reprex package (v0.3.0)

Thanks technocrat this is exactly what I need. I had a follow up question though:

How could I take, for example, the 4th quantile, first number (or both numbers; 57.454, 73.368) and store it in a variable without manually typing this in?

I tried looking through the help section of classIntervals and I couldnt find it.

1 Like

Duh, I got it!

X <- classIntervals(jenks71$jenks71, n=5, style="quantile"))
X$brks[5]
1 Like

Sometimes coming up with the right question is all one needs. :grin:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.