Geom_histogram: max bin height

ggplot2

#1

Very simple question: I would like to know the max bin height in a histogram. My call is

ggp <- ggplot(data = xq, aes(xp)) + geom_histogram(binwidth = 0.02,
boundary = 0)

where xq is a data frame containing xp. This makes a nice histogram, but I haven't been able to discover how to find the max bin height without reproducing the entire histogram myself, which is obviously possible but undesirable.

I am working on an iMac using RStudio.

Thanks in advance,

George Yost, Ph. D.


#2

I'm not sure I'm totally understanding your question. Are you trying to get the underlying statistic without creating a plot? Or maybe you want to know the values that are used in the plot?

For the latter, you could use the ggplot_build function to see "under the hood" in the plot.

library(tidyverse)
ggp <- ggplot(iris, aes(Sepal.Width)) + 
  geom_histogram(binwidth = 0.02, boundary = 0)
ggp 

# This object will expose the ingredients for the plot.
ggp_build <- ggplot_build(ggp)
ggp_build

# In the RStudio viewing pane, I explored the object and found the
# numbers for the count. Clicking the floating icon at the right copied the
# coordinates for that vector to the console.
ggp_data[["data"]][[1]][["count"]]

# As for the tallest bin height:
max(ggp_data[["data"]][[1]][["count"]])

#3

jonspring -

I need it to add a vertical line to the plot. Your fix works! Thanks. Appreciate the help.

  • George