ggplot2 find number of counts in histogram maximum

后端 未结 2 1709
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 03:19

I have a short R script which plots a few histograms using ggplot2. How can I automatically set the ymax limit in the histogram based on the maximum frequency in the histogr

2条回答
  •  长情又很酷
    2020-12-16 04:19

    Personally, I find the 'hist' function to be the most useful for these sorts of calculations. The 'hist' function is super fast and can provide your frequency counts. For your case, you could do something like this:

    max(hist(data$myo_activity, breaks=seq(range_Min, range_Max, by=bin_Width), plot=FALSE)$counts)
    

    Where range_Min is the bottom of your theoretical range (i.e. 0), and range_Max is the upper limit above your theoretically range. bin_Width is the value width of each frequency count.

    The equation should give you the max value you need to specify the plot range. I believe the 'ggplot' function is calling the 'hist' function anyway, so I prefer to call it directly when I'm only wanting the data.

提交回复
热议问题