R histogram - frequency range

I am trying to get the maximum frequency on a bar graph. I have a list of values. Then I do the following:

hist(list, breaks=length(list), freq=TRUE) 

and it automatically creates ranges for the x and y axis. The y axis is the frequency, and the x axis is the value in the list.

So, how can I find the maximum frequency that will be displayed on this graph?

I am trying to make a legend in the upper right corner of my graph, so I need to get the maximum frequency value. Or is there a way to tell R to put the legend field in the upper right corner of the graph?

+4
source share
4 answers

The histogram values ​​can be saved as a data frame in R. Taking the example “list” of data “Example”, you could:

 list_histo <- hist(list, breaks=length(list), freq=TRUE) 

just typing

 list_histo 

back in R a new “meta” data frame will be shown containing histogram information (the data shown here are arbitrary and for illustration):

 $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 highest value is now simple - just using

 max(list_histo$counts) 

will return the maximum value.

+6
source
 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 gaps, the width of the stripes will vary, but the average values ​​will give you the midpoint of the panel you are looking for. This finds the middle of the bar with the highest counter (maximum frequency)

+2
source

Instead of the x, y arguments for the legend, you can use legend('topright',...)

+2
source

You can also use table(list)

It will return a list of values ​​and errors that they repeat:

 > list<-c(0.2, 0.6, 0.4, 0.5, 0.1, 0.5, 0.6, 0.6, 0.6, 0.1, 0.1, 0.6, 0.6, 0.6, 0.6) > table(list) list 0.1 0.2 0.4 0.5 0.6 3 1 1 2 8 > max(table(list)) [1] 8 
0
source

All Articles