[R] how to plot 95% confidential interval as vertical lines to x axe in density plot

Mao Jianfeng jianfeng.mao at gmail.com
Fri Aug 21 14:14:24 CEST 2009


Dear R-help listers,

Under the helps from the ggplot2 list, I have set up method of drawing
a graph with multiple density plots arranged one by one in one page.

Now, I want to add 95% confidential interval as vertical lines to x
axe in density plot. I have found the library(hdrcde) can do this
work, but I do not know
how to handle functions of this library when I used ggplot2 to draw the graph.

Thank you in advance.

The data and codes followed:

# dummy data
factor<-rep(c("Alice","Jone","Mike"),each=100)
factor<-factor(factor)
traits1<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits2<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits3<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits4<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits5<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits6<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits7<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits8<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits9<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits10<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits11<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits12<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits13<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits14<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits15<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits16<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits17<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits18<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits19<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits20<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))
traits21<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3),
rnorm(100, mean=6, sd=6))

myda<-data.frame(factor,traits1,traits2,traits3,traits4,traits5,traits6,traits7,traits8,traits9,traits10,traits11,traits12,traits13,traits14,traits15,traits16,traits17,traits18,
traits19,traits20,traits21)


library(ggplot2)
d = melt(myda, id = "factor")

str(d)

pdf("test33.pdf")
p =
ggplot(data=d, mapping=aes(x=value, y=..density..)) +
facet_wrap(~variable)+
stat_density(aes(fill=factor), alpha=0.5, col=NA, position = 'identity') +
stat_density(aes(colour = factor), geom="path", position = 'identity')
print(p)
dev.off()

Mao J-F




More information about the R-help mailing list