I'm trying to get the maximum frequency on a histogram graph. I have a list of values. Then, I do the following:
hist(list, breaks=length(list), freq=TRUE)
and it automatically makes the ranges for the x and y axis. The y axis is the frequencies, and the x axis is the values in the list.
So, how can I find the maximum frequency that will show up on this graph?
I'm trying to make a legend in the top right corner of my graph, so I need to get the maximum frequency value. Or is there a way to tell R to put a legend box in the top right corner of a graph?
The values of a histogram can be stored as a data frame in R. Taking the OP's example dataframe 'list', you could:
list_histo <- hist(list, breaks=length(list), freq=TRUE)
simply typing
list_histo
back into R will show the new 'meta' data frame containing information about the histogram (data shown here is arbitrary and for illustration purposes):
$breaks
[1] 0.40 0.42 0.44 0.46 0.48 0.50 0.52 0.54 0.56 0.58 0.60 0.62 0.64 0.66 0.68
[16] 0.70 0.72 0.74 0.76
$counts
[1] 1 15 112 878 4734 17995 51094 110146 178855 216454
[11] 194536 130591 64218 23017 6117 1070 144 23
$intensities
[1] 0.00005 0.00075 0.00560 0.04390 0.23670 0.89975 2.55470 5.50730
[9] 8.94275 10.82270 9.72680 6.52955 3.21090 1.15085 0.30585 0.05350
[17] 0.00720 0.00115
$density
[1] 0.00005 0.00075 0.00560 0.04390 0.23670 0.89975 2.55470 5.50730
[9] 8.94275 10.82270 9.72680 6.52955 3.21090 1.15085 0.30585 0.05350
[17] 0.00720 0.00115
$mids
[1] 0.41 0.43 0.45 0.47 0.49 0.51 0.53 0.55 0.57 0.59 0.61 0.63 0.65 0.67 0.69
[16] 0.71 0.73 0.75
$xname
[1] "list_histo"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
calling the largest value is now straightforward -- simply using
max(list_histo$counts)
will return the maximum value.
set.seed(100)
x = rnorm(100, mean = 5, sd = 2)
res = hist(x)
res$mids[which.max(res$counts)]
[1] 4.5
Depending on the breaks the widths of the bars will change, but the mids will give you the midpoint of the bar you are looking for. This finds the midpoint of the bar with the highest count (maximum frequency)
Instead of x, y arguments to legend, you can use legend('topright',...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With