python
python

Reputation: 4521

Draw vertical peak lines in histogram using qplot() in R

I using the diamonds dataset which comes with ggplot2 and creating histograms of price field. You could load the dataset using

install.packages(ggplot2)
data(diamonds)

I am trying to analyze the peak values in the histogram which I created using this line

qplot(price, data = diamonds, geom = "histogram", col = 'blues') 

enter image description here

I want to draw a peak line in this histogram and find whats the value. I have explored couple of questions here but none is working with qplot. Could anyone suggest how can I draw lines at peak values of histogram.

Upvotes: 1

Views: 1193

Answers (1)

Rorschach
Rorschach

Reputation: 32446

The manual way: you can extract the histogram information using ggplot_build. Then find the maximum y-value, and the x-location of the corresponding bar in the histogram.

library(ggplot2)
data(diamonds)

## The plot as you have it
q <- qplot(price, data = diamonds, geom = "histogram", col = 'blues')

## Get the histogram info/the location of the highest peak
stuff <- ggplot_build(q)[[1]][[1]]

## x-location of maxium
x <- mean(unlist(stuff[which.max(stuff$ymax), c("xmin", "xmax")]))

## draw plot with line
q + geom_vline(xintercept=x, col="steelblue", lty=2, lwd=2)

enter image description here

The y-value at that location is

## Get the y-value
max(stuff$ymax)
# [1] 13256

Another option using stat_bin, should give the same results as above, but it's more obscure because of the hidden variables.

q + stat_bin(aes(xend=ifelse(..count..==max(..count..), ..x.., NA)), geom="vline",
             color="steelblue", lwd=2, lty=2)

Upvotes: 3

Related Questions