开发者

R histogram - range of frequencies

开发者 https://www.devze.com 2023-02-26 08:25 出处:网络
I\'m trying to get the maximum frequency on a histogram graph. I have a list of values. Then, I do the following:

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开发者_如何学C, 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',...)


You can also use table(list)

It will return a list of the values and tumber of times they are repeated:

> 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

精彩评论

暂无评论...
验证码 换一张
取 消