Hi All, I generated 5000 samples using the following script test<- rnorm(5000,1000,100) test1 <- subset(test, subset=(test > 1100)) d <- density(test) plot(d, main="Density of production") abline(v=mean(test1) I wanted to do the following but faced difficulties 1. to shade or color (blue) the curve using the criterion that any values greater than 11,000 2. I drew a vertical line but I wanted the v-line within the curve not to stick outside the curve 3. to suppress the output produced at the bottom of the curve( N=5000 and bandwidth =16.22) Thanks in advance Val [[alternative HTML version deleted]]
Here's one way to do what I think you want: test<- rnorm(5000,1000,100) test1 <- subset(test, subset=(test > 1100)) d <- density(test) plot(d, main="Density of production", xlab="") lines(d$x[d$x > 1100], d$y[d$x > 1100], col="blue", lwd=2) curveheight <- d$y[abs((d$x - mean(test1))) == min(abs((d$x - mean(test1))))] segments(x0=mean(test1), y0=0, y1=curveheight) Sarah On Mon, Dec 13, 2010 at 1:44 PM, Val <valkremk at gmail.com> wrote:> Hi All, > > ?I generated 5000 samples using the following script > > ? ? ? ?test<- rnorm(5000,1000,100) > ? ? ? ?test1 <- subset(test, subset=(test > 1100)) > ? ? ? ?d <- density(test) > ? ? ? ?plot(d, main="Density of production") > ? ? ? ?abline(v=mean(test1) > > I wanted to do the following but faced difficulties > 1. to shade or color (blue) the curve using the criterion that any values > greater than 11,000 > 2. I drew a vertical line ?but I wanted the v-line within the curve not to > stick outside the curve > 3. to suppress the output ?produced ?at the bottom of the curve( N=5000 and > bandwidth =16.22) > > Thanks ?in advance > ?Val >-- Sarah Goslee functionaldiversity.org
Val, Here's the complete console output. The graph produced is at: functionaldiversity.org/temp/curve.png R version 2.12.0 (2010-10-15) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: x86_64-redhat-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.> ls()character(0)> > test<- rnorm(5000,1000,100) > test1 <- subset(test, subset=(test > 1100)) > d <- density(test) > > png("curve.png") > plot(d, main="Density of production", xlab="") > > xarea <- c(1100, d$x[d$x > 1100], c(max(test))) > yarea <- c(0, d$y[d$x > 1100], 0) > > > polygon(xarea, yarea, col="blue") > > curveheight <- d$y[abs((d$x - mean(test1))) == min(abs((d$x - mean(test1))))] > segments(x0=mean(test1), y0=0, x1=mean(test1), y1=curveheight, col="red", lwd=2) > dev.off()null device 1>Sarah On Tue, Dec 14, 2010 at 8:59 AM, Val <valkremk at gmail.com> wrote:> Hi Sara, > > Could you please send me your output as an attached file if it is possible? > > Thanks > > >> >> 1. to shade or color (blue) the curve using the criterion that any >> >> values >> >> greater than 11,000 >> > >> > I think I was not clear in the above point. I want shade not the line >> > but >> > the area under the curve, >> >> Here's an example of how to do that using polygon: >> addictedtor.free.fr/graphiques/RGraphGallery.php?graph=7 >> >> > and >> > Your last line of code, >> > segments(x0=mean(test1), y0=0, y1=curveheight) >> > >> > gave me the? following error message >> > >> > Error in segments(x0 = mean(test1), y0 = 0, y1 = curveheight) : >> > ?element 3 is empty; >> > ? the part of the args list of '.Internal' being evaluated was: >> > ? (x0, y0, x1, y1, col = col, lty = lty, lwd = lwd, ...) >> > >> > could you check it please >> >> I checked it before I sent it to you. The code I provided works correctly >> on my computer. ?(R 2.12.0, Linux). >> >> You could try this statement instead: >> segments(x0 = mean(test1), y0 = 0, x1=mean(test1), y1 = curveheight) >> >> Sarah >> >> ---- Sarah Goslee functionaldiversity.org
You are most likely to get reasonable help if you provide at a minimum your OS, the version of R you're using, and the error message you get. And also if you send your replies to the R-help list, not just me. Sarah On Tue, Dec 14, 2010 at 10:58 AM, Val <valkremk at gmail.com> wrote:> Thank you Sarah, > > > It worked in my Linux machine as well? but not in Windows. > > > > > > On Tue, Dec 14, 2010 at 9:37 AM, Sarah Goslee <sarah.goslee at gmail.com> > wrote: >> >> Val, >> >> Here's the complete console output. The graph produced is at: >> functionaldiversity.org/temp/curve.png >> >> R version 2.12.0 (2010-10-15) >> Copyright (C) 2010 The R Foundation for Statistical Computing >> ISBN 3-900051-07-0 >> Platform: x86_64-redhat-linux-gnu (64-bit) >> >> R is free software and comes with ABSOLUTELY NO WARRANTY. >> You are welcome to redistribute it under certain conditions. >> Type 'license()' or 'licence()' for distribution details. >> >> ?Natural language support but running in an English locale >> >> R is a collaborative project with many contributors. >> Type 'contributors()' for more information and >> 'citation()' on how to cite R or R packages in publications. >> >> Type 'demo()' for some demos, 'help()' for on-line help, or >> 'help.start()' for an HTML browser interface to help. >> Type 'q()' to quit R. >> >> > ls() >> character(0) >> > >> > test<- rnorm(5000,1000,100) >> > test1 <- subset(test, subset=(test > 1100)) >> > d <- density(test) >> > >> > png("curve.png") >> > plot(d, main="Density of production", xlab="") >> > >> > xarea <- c(1100, d$x[d$x > 1100], c(max(test))) >> > yarea <- c(0, d$y[d$x > 1100], 0) >> > >> > >> > polygon(xarea, yarea, col="blue") >> > >> > curveheight <- d$y[abs((d$x - mean(test1))) == min(abs((d$x - >> > mean(test1))))] >> > segments(x0=mean(test1), y0=0, x1=mean(test1), y1=curveheight, >> > col="red", lwd=2) >> > dev.off() >> null device >> ? ? ? ? ?1 >> > >> >> Sarah >> >> >> On Tue, Dec 14, 2010 at 8:59 AM, Val <valkremk at gmail.com> wrote: >> > Hi Sara, >> > >> > Could you please send me your output as an attached file if it is >> > possible? >> > >> > Thanks >> > >> > >> >> >> 1. to shade or color (blue) the curve using the criterion that any >> >> >> values >> >> >> greater than 11,000 >> >> > >> >> > I think I was not clear in the above point. I want shade not the line >> >> > but >> >> > the area under the curve, >> >> >> >> Here's an example of how to do that using polygon: >> >> addictedtor.free.fr/graphiques/RGraphGallery.php?graph=7 >> >> >> >> > and >> >> > Your last line of code, >> >> > segments(x0=mean(test1), y0=0, y1=curveheight) >> >> > >> >> > gave me the? following error message >> >> > >> >> > Error in segments(x0 = mean(test1), y0 = 0, y1 = curveheight) : >> >> > ?element 3 is empty; >> >> > ? the part of the args list of '.Internal' being evaluated was: >> >> > ? (x0, y0, x1, y1, col = col, lty = lty, lwd = lwd, ...) >> >> > >> >> > could you check it please >> >> >> >> I checked it before I sent it to you. The code I provided works >> >> correctly >> >> on my computer. ?(R 2.12.0, Linux). >> >> >> >> You could try this statement instead: >> >> segments(x0 = mean(test1), y0 = 0, x1=mean(test1), y1 = curveheight) >> >> >> >> Sarah >> >> >> >> -- >> >> >> >> -- >> Sarah Goslee >> functionaldiversity.org > >-- Sarah Goslee stringpage.com astronomicum.com functionaldiversity.org