Mao Jianfeng
2009-Aug-19 08:05 UTC
[R] how to fill the area under the density line with semitransparent colors
Dear R-listers, I have created a plot to display the density lines for the same variable by different entities. Now, I want to fill the area under the density lines with semitransparent colors. Though I have checked that in web-searching and book-reading, I still do not perform that. Could anyone please give me any helps or advice? Thank you in advance. The data and code I used listed below: # dummy data factor<-rep(c("Alice","Jone","Mike"),each=100) factor<-factor(factor) traits<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3), rnorm(100, mean=6, sd=6)) myda<-data.frame(factor,traits) # my plot plot(c(min(myda$traits),max(myda$traits)),c(-0.03,0.5), xlab='State', ylab='ylab') lines(density(myda$traits[factor==c("Alice")]), lwd=2,col=2) lines(density(myda$traits[factor==c("Jone")]), lwd=2,col=3) lines(density(myda$traits[factor==c("Mike")]), lwd=2,col=4) points(myda$traits[factor==c("Alice")], rep(-0.01, length(myda$traits[factor==c("Alice")])), pch="|", col=2) points(myda$traits[factor==c("Jone")], rep(-0.02, length(myda$traits[factor==c("Jone")])), pch="|", col=3) points(myda$traits[factor==c("Mike")], rep(-0.03, length(myda$traits[factor==c("Mike")])), pch="|", col=4) Regards, Yours Mao J-F
baptiste auguie
2009-Aug-19 08:40 UTC
[R] how to fill the area under the density line with semitransparent colors
Hi, One way using ggplot2, library(ggplot2) ggplot(data=myda, mapping=aes(x=traits, y=..density..)) + stat_density(aes(fill=factor), alpha=0.5, col=NA, position = 'identity') + stat_density(aes(colour = factor), geom="path", position = 'identity', size=2) HTH, baptiste 2009/8/19 Mao Jianfeng <jianfeng.mao at gmail.com>:> Dear R-listers, > > I have created a plot to display the density lines for the same > variable by different entities. Now, I want to fill the area under the > density lines with semitransparent colors. > Though I have checked that in web-searching and book-reading, I still > do not perform that. > > Could anyone please give me any helps or advice? Thank you in advance. > > The data and code I used listed below: > > # dummy data > factor<-rep(c("Alice","Jone","Mike"),each=100) > factor<-factor(factor) > traits<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3), > rnorm(100, mean=6, sd=6)) > myda<-data.frame(factor,traits) > > # my plot > plot(c(min(myda$traits),max(myda$traits)),c(-0.03,0.5), xlab='State', > ylab='ylab') > lines(density(myda$traits[factor==c("Alice")]), lwd=2,col=2) > lines(density(myda$traits[factor==c("Jone")]), lwd=2,col=3) > lines(density(myda$traits[factor==c("Mike")]), lwd=2,col=4) > points(myda$traits[factor==c("Alice")], rep(-0.01, > length(myda$traits[factor==c("Alice")])), pch="|", col=2) > points(myda$traits[factor==c("Jone")], rep(-0.02, > length(myda$traits[factor==c("Jone")])), pch="|", col=3) > points(myda$traits[factor==c("Mike")], rep(-0.03, > length(myda$traits[factor==c("Mike")])), pch="|", col=4) > > Regards, > > Yours > Mao J-F > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- _____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag
David Winsemius
2009-Aug-19 10:54 UTC
[R] how to fill the area under the density line with semitransparent colors
On Aug 19, 2009, at 4:05 AM, Mao Jianfeng wrote:> Dear R-listers, > > I have created a plot to display the density lines for the same > variable by different entities. Now, I want to fill the area under the > density lines with semitransparent colors. > Though I have checked that in web-searching and book-reading, I still > do not perform that. > > Could anyone please give me any helps or advice? Thank you in advance. > > The data and code I used listed below: > > # dummy data > factor<-rep(c("Alice","Jone","Mike"),each=100) > factor<-factor(factor) > traits<-c(rnorm(100, mean=1, sd=1), rnorm(100, mean=3, sd=3), > rnorm(100, mean=6, sd=6)) > myda<-data.frame(factor,traits) > > # my plot > plot(c(min(myda$traits),max(myda$traits)),c(-0.03,0.5), xlab='State', > ylab='ylab') > lines(density(myda$traits[factor==c("Alice")]), lwd=2,col=2) > lines(density(myda$traits[factor==c("Jone")]), lwd=2,col=3) > lines(density(myda$traits[factor==c("Mike")]), lwd=2,col=4) > points(myda$traits[factor==c("Alice")], rep(-0.01, > length(myda$traits[factor==c("Alice")])), pch="|", col=2) > points(myda$traits[factor==c("Jone")], rep(-0.02, > length(myda$traits[factor==c("Jone")])), pch="|", col=3) > points(myda$traits[factor==c("Mike")], rep(-0.03, > length(myda$traits[factor==c("Mike")])), pch="|", col=4)Searching with the terms "polygon density(" produces a worked example of filling the area under a density plot at this help page: http://finzi.psych.upenn.edu/R/library/mclust02/html/density.html -- David Winsemius, MD Heritage Laboratories West Hartford, CT
Possibly Parallel Threads
- Why are there small circles in my plot
- how to add 95% confidential interval as vertical lines to x axe in density plot
- how to substitute missing values (NAs) by the group means
- how to calculate the consistency of different clusterings
- how to use "lapplyBy" function of "doBy" package