tag:blogger.com,1999:blog-7556813435224291579.post2287414331769943877..comments2023-03-23T10:46:08.543+00:00Comments on One R Tip A Day: Highlight overlapping area between two curvesPaolohttp://www.blogger.com/profile/01969817827028660433noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-7556813435224291579.post-87149783617238884662013-08-16T16:14:38.578+01:002013-08-16T16:14:38.578+01:00Dear Yoonjeong,
Not a solution, but a starting poi...Dear Yoonjeong,<br />Not a solution, but a starting point:<br /><br />library(ggplot2)<br />ggplot()+ geom_density(aes(x=y1), colour="red", fill="red", alpha=0.3) + geom_density(aes(x=y2), colour="blue", fill="blue", alpha=0.3)<br /><br />HIH!Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-88562800151607498692013-08-05T23:35:34.330+01:002013-08-05T23:35:34.330+01:00Hello. This is great R code but I cannot use the a...Hello. This is great R code but I cannot use the above code because I transform data (please see below). Is there any way to highlight overlapping area between two curves?<br /><br />... such like<br />a1=-0.116977231<br />b1=0.776585015<br />c1=0.116977231<br />d1=0.065499503<br /><br />y1=rnorm(1000000,0,1)<br />y1=a1+b1*y1+c1*y1^2+d1*y1^3<br />y2=rnorm(1000000,0,1)<br />y2=a1+b1*y2+c1*y2^2+d1*y2^3<br />y2=2.5*y2+0.5<br /><br />plot(density(y1), xlim= c(-7, 7), ylim =c(0, 0.75), col="black", lwd=2, lty=1, xlab=" ", main="",bty="l" )<br />lines(density(y2), xlim= c(-7, 7), ylim =c(0, 0.75), col="black", lwd=2, lty=2, xlab=" ", main="")<br /><br />Many thanks..<br /><br />Yoonjeong<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-9413831796797614352008-09-14T16:25:00.000+01:002008-09-14T16:25:00.000+01:00You're too kind! :-S Glad I was useful! Thanks for...You're too kind! :-S <BR/>Glad I was useful! Thanks for your contribution! I hope that someone else will find our effort useful.Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-18528842757734422002008-09-13T18:50:00.000+01:002008-09-13T18:50:00.000+01:00Paolo, you're a genius! Thank you for all your hel...Paolo, you're a genius! Thank you for all your help and recommendations. I look forward to chatting in the future.<BR/><BR/>Best wishes,<BR/>Ryanrtwilliahttps://www.blogger.com/profile/09745132810140181185noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-66811375582901635662008-09-11T08:50:00.000+01:002008-09-11T08:50:00.000+01:00This should fit your need:p = seq(-8.5,4.9,.001)x1...This should fit your need:<BR/><BR/>p = seq(-8.5,4.9,.001)<BR/>x1 <- dnorm(p,-1.5,1.5)<BR/>x2 <- dnorm(p,0,1.5)<BR/>plot(range(p),range(x1,x2),type="n")<BR/>lines(p,x1,col="red",lwd=4)<BR/>lines(p,x2,col="blue",lwd=4,lty=2)<BR/>line <-- 1.13<BR/>abline(v=line)<BR/><BR/>xt1 <- c(p[1], p[p < line], line)<BR/>yt1 <- c(x1[1], x1[p < line],0)<BR/>polygon(xt1, yt1, col="grey")Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-51221796941664962062008-09-10T03:39:00.000+01:002008-09-10T03:39:00.000+01:00Hey Paolo,Thanks for getting back to me. Close. I...Hey Paolo,<BR/>Thanks for getting back to me. Close. I basically used brute force to get to the image I needed although in, I'm sure, a most inefficient manner. I wanted shading on curve x1 from -1.13 to the remainder of the negative tail of the distribution. Maybe you can take a look at at it ans see if you can tell me how one would get this result without using "white" polygon fill to achieve the graphic. You'll see what I'm talking about.<BR/>Thanks for all of your advice,<BR/>Ryan<BR/><BR/>p<-seq(-8.5,4.9,.001)<BR/>x1<-dnorm(p,-1.5,1.5)<BR/>x2<-dnorm(p,0,1.5)<BR/>plot(range(p),range(x1,x2),type="n")<BR/>lines(p,x1,col="red",lwd=4)<BR/>lines(p,x2,col="blue",lwd=4,lty=2)<BR/>line<--1.13<BR/>#abline(v=line)<BR/><BR/>x<-p<BR/>y<-x1<BR/>scale<-.001<BR/>shade<- (max(y)) / scale<BR/>xt <- c(x[(shade):length(x)],-1)<BR/>yt <- c(y[(shade):length(y)],0)<BR/>polygon(xt, yt, col="gray")<BR/>lines(p,x1,col="red",lwd=4)<BR/>lines(p,x2,col="blue",lwd=4,lty=2)<BR/><BR/>x<-p<BR/>y<-x1<BR/>scale<-.001<BR/>cutpoint <- (max(x)-line) / scale<BR/>xt <- c(x[(length(x)-cutpoint):length(x)],line)<BR/>yt <- c(y[(length(y)-cutpoint):length(y)],0)<BR/>polygon(xt, yt, col='white')<BR/>lines(p,x1,col="red",lwd=4)<BR/>lines(p,x2,col="blue",lwd=4,lty=2)<BR/>lines(p,x1,col="red",lwd=4)<BR/>floor<-0<BR/>abline(h=floor)rtwilliahttps://www.blogger.com/profile/09745132810140181185noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-9188728123759677002008-09-09T13:56:00.000+01:002008-09-09T13:56:00.000+01:00I'm not at all a guru! I like R because, nine ...I'm not at all a guru! I like R because, nine times out of ten, you can solve problems in short time and, of course, because of his awesome community.<BR/>I'm not sure if this is what you're looking for...<BR/>##<BR/>xt1 <- c(x[(length(x)-cutpoint):length(x)], line)<BR/>y1=pmin(x1,x2)<BR/>yt1 <- c(y1[(length(y1)-cutpoint):length(y1)],0)<BR/>polygon(xt1,yt1, density=50)<BR/>##<BR/><BR/>I advice you to take a look at the package HH, the function normal.and.t.dist() may be of particular interest to you!Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-31559457773782526252008-09-09T04:16:00.000+01:002008-09-09T04:16:00.000+01:00Will do! Would you mind glancing at my syntax her...Will do! Would you mind glancing at my syntax here? I am trying to shade an area under two curves.<BR/><BR/>#Two curves with shading<BR/>p<-seq(-6,9.5,.001)<BR/>x1<-dnorm(p,-1.5,1.5)<BR/>x2<-dnorm(p,0,1.5)<BR/>plot(range(p),range(x1,x2),type="n")<BR/>lines(p,x1,col="red",lwd=4)<BR/>lines(p,x2,col="blue",lwd=4)<BR/>line<- -1.5<BR/>abline(v=line)<BR/>x<-p<BR/>y<-x1<BR/>scale<-.001<BR/>cutpoint <- (max(x) - line) / scale<BR/>xt <- c(x[(length(x)-cutpoint):length(x)],line)<BR/>yt <- c(y[(length(y)-cutpoint):length(y)],0)<BR/>polygon(xt, yt, density=50)<BR/>lines(p,x1,col="red",lwd=4)<BR/>lines(p,x2,col="blue",lwd=4)<BR/><BR/>#As silly as this seems, I can't seem to get the shading to go in the negative direction. Please help, R guru!rtwilliahttps://www.blogger.com/profile/09745132810140181185noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-22475488689029589352008-09-08T09:39:00.000+01:002008-09-08T09:39:00.000+01:00I'm glad that my simple tips are of some use for y...I'm glad that my simple tips are of some use for you! An advice: subscribe to the R-help mailing list (http://www.r-project.org/mail.html) it is really a never-ending source of GOOD information, code and inspiration!Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-78600726612611890152008-09-06T22:36:00.000+01:002008-09-06T22:36:00.000+01:00Thank you very much for this post. I am an R novi...Thank you very much for this post. I am an R novice but this is really helpful for me.rtwilliahttps://www.blogger.com/profile/09745132810140181185noreply@blogger.com