I'm assuming you want to identify the single largest peak in each facet – this would be the mode of the distribution. If your distribution is multimodal, my answer will only identify the largest peak. This answer to another question explains that geom_density()
uses the density()
function w/ default arguments.
That being said, the following code should work for you:
library(ggplot2)
library(grid)
library(plyr)
data <- data.frame("frame"=c(rep("A",9), rep("B", 13), rep("C", 7)), "val"=c(1,rep(2,4),4,5,6,rep(1,6),2,rep(3,7),1,rep(4,6)))
attach(data)
densMode <- function(x){
td <- density(x)
maxDens <- which.max(td$y)
list(x=td$x[maxDens], y=td$y[maxDens])
}
xdat <- ddply(data,"frame", transform, val_mean = signif(densMode(val)$x,3), med.x = signif(densMode(val)$x,3), med.y=signif(densMode(val)$y,3))
hp <- ggplot(data=data, aes(x=val)) +
geom_density() +
geom_vline(aes(xintercept=val_mean),xdat, color="red",linetype="dashed",size=1) +
theme_bw()
hp<- hp +
facet_wrap (~ frame, ncol=2, scales="free_y") +
geom_text(data = xdat, aes(x=med.x,y=med.y,label=val_mean))
hp
The only lines I changed were those that determined how the graph was created (I didn't use png()
), inserting the densMode()
function, and using densMode()
in the definition of xdat
. I also created a data.frame based on your example data (which I've submitted as an edit to your question, for the convenience of others who may want to answer).
The code produces the following figure: