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 histogram (plus 10%) i.e
scale_y_continuous(limits= c(0,ymax*1.1)
plot = ggplot(data, aes(myo_activity)) +
geom_histogram(binwidth=0.5, aes(fill=..count..))
plot + scale_x_continuous(expand = c(0,0), limits = c(30,90)) +
scale_y_continuous(expand = c(0,0), limits = c(0,140))
For example used data
moviesas sample data are not provided.With function
ggplot_build()you can get list containing all the elements used for plotting your data. All the data are in list elementdata[[1]]. Columncountof this element contains values for histogram. You can use maximal value of this column to set limits for your plot.