I am trying to plot distributions/densities of data, using ggplot within facets. Here is what I have right now where the red line shows the mean with mean value shown in each facet. Now here, mean values do not make sense, I wish to have similar plotting where peak values in the density are shown with xintercept and text.
The code I used for the means is this:
data <- read.table("sample.csv", header=F, sep=',')
colnames(data) <- c("frame", "val")
attach(data)
library(ggplot2)
library(grid)
library(plyr)
xdat <- ddply(data,"frame", transform, val_mean = signif(mean(val),3), med.x = signif(mean(val),3), med.y=signif(mean(density(val)$y),3))
ppi <- 500
png("sample.png", width=4*ppi, height=4*ppi, res=ppi)
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))
print(hp)
dev.off()
and the data used to plot this graph are:
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))
)
I know that there have been some posts where R has been used to find peaks in the values. But I wish to plot peaks in the densities and I am not able to find any solution for it (or maybe I missed it). Is it possible to calculate peaks on-the-fly in R and plot within different facets? Thank you very much in advance for your time and help!!