Hi all, for example:> X<- rnorm(1000) > X0 <- 0.899I want to draw a density distribution plot with a arrow pointer indicating the position of X0, meanwhile, giving out the p-value. any functions? Thanks very much.
Jim Brennan
2005-Jun-15 15:39 UTC
[R] how to plot density distribution with a arrow pointer?
This is one way not so good R>X<-rnorm(1000000) R>X0<-.899 R>plot(density(X)) R>abline(v=X0) A better way R>x<-seq(-5,5,.01) R>plot(x,dnorm(x)) R>plot(x,dnorm(x),type="l",col=2) R>abline(v=X0,col=4) R>?text R>text(X0,.2,paste("P(X<X0) =",signif(pnorm(X0),3)),pos=2) R>abline(v=X0,col=4) If you actually want arrows do ?arrows Jim -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Hu Chen Sent: June 15, 2005 10:41 AM To: R Subject: [R] how to plot density distribution with a arrow pointer? Hi all, for example:> X<- rnorm(1000) > X0 <- 0.899I want to draw a density distribution plot with a arrow pointer indicating the position of X0, meanwhile, giving out the p-value. any functions? Thanks very much. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Thanks. A small arrow may be more nice-looking than a long cutting line. and, another question, how to get the p-value of the position of X0? If the whole distribution is not as regular as norm, chi-square but a strange one, how could I get the X0's p-value? any functions? Thanks very much. On 6/15/05, Jim Brennan <jfbrennan at rogers.com> wrote:> This is one way not so good > R>X<-rnorm(1000000) > R>X0<-.899 > R>plot(density(X)) > R>abline(v=X0) > > A better way > > R>x<-seq(-5,5,.01) > R>plot(x,dnorm(x)) > R>plot(x,dnorm(x),type="l",col=2) > R>abline(v=X0,col=4) > R>?text > R>text(X0,.2,paste("P(X<X0) =",signif(pnorm(X0),3)),pos=2) > R>abline(v=X0,col=4) > > If you actually want arrows do ?arrows > Jim > > -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Hu Chen > Sent: June 15, 2005 10:41 AM > To: R > Subject: [R] how to plot density distribution with a arrow pointer? > > Hi all, > for example: > > X<- rnorm(1000) > > X0 <- 0.899 > > I want to draw a density distribution plot with a arrow pointer > indicating the position of X0, meanwhile, giving out the p-value. > > any functions? > > Thanks very much. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Gabor Grothendieck
2005-Jun-17 03:25 UTC
[R] how to plot density distribution with a arrow pointer?
Sorry, I copied the first part from the previous post and the second part from my editor session and must have not copied the entire script. Here it is again: # next 5 lines from previous post: x0 <- 0.899 x <- c( rnorm(6000), rnorm(4000, mean=3) ) plot( d <- density(x) ) y0 <- approx( d$x, d$y, xout=x0 )$y # height at x0 # segments( x0, 0, x0, y0, col=2 ) vf <- c("serif", "plain") points(x0,y0, pch=20, col="red") # red dot text(x0,y0, "\\da",vfont=vf, pos=3, col="green") text(x0,y0, "\\ua",vfont=vf, pos=1, col="red") yax <- par("usr")[3] # xaxis was drawn at this y level text(x0,yax, "\\ua", vfont=vf, pos=1, col="blue", xpd=TRUE, offset=0) text(x0,yax, "\\da", vfont=vf, pos=3, col="red", xpd=TRUE, offset=0) On 6/16/05, Jim Brennan <jfbrennan at rogers.com> wrote:> I am trying to reproduce your example but we don't have object vf in > vfont=vf. Do we need some package? > > R>text(x0,y0, "\\da",vfont=vf, pos=3, col="green") > Error in text.default(x0, y0, "\\da", vfont = vf, pos = 3, col = "green") : > Object "vf" not found > R>version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 1.0 > year 2005 > month 04 > day 18 > language R > > -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gabor Grothendieck > Sent: June 16, 2005 10:56 PM > To: ramasamy at cancer.org.uk > Cc: R > Subject: Re: [R] how to plot density distribution with a arrow pointer? > > On 6/16/05, Adaikalavan Ramasamy <ramasamy at cancer.org.uk> wrote: > > I am assuming that you want to do this empirically : > > > > x0 <- 0.899 > > x <- c( rnorm(6000), rnorm(4000, mean=3) ) > > plot( d <- density(x) ) > > > > y0 <- approx( d$x, d$y, xout=x0 )$y # height at x0 > > > > segments( x0, 0, x0, y0, col=2 ) > > > > > The following displays 4 arrows. Choose the one you > like best: > > # next 5 lines from previous post: > x0 <- 0.899 > x <- c( rnorm(6000), rnorm(4000, mean=3) ) > plot( d <- density(x) ) > y0 <- approx( d$x, d$y, xout=x0 )$y # height at x0 > # segments( x0, 0, x0, y0, col=2 ) > > points(x0,y0, pch=20, col="red") # red dot > text(x0,y0, "\\da",vfont=vf, pos=3, col="green") > text(x0,y0, "\\ua",vfont=vf, pos=1, col="red") > yax <- par("usr")[3] # xaxis was drawn at this y level > text(x0,yax, "\\ua", vfont=vf, pos=1, col="blue", xpd=TRUE, offset=0) > text(x0,yax, "\\da", vfont=vf, pos=3, col="red", xpd=TRUE, offset=0) > > The above uses xpd= which allows drawing outside of the main > plot area and usr= which we need to get the position of the > y coordinate of the boundary. It also uses the Hershey font > for the arrow. Other arguments are described on the > appropriate page. See: > > ?points > ?text > ?par # info on xpd= and usr> ?Hershey > demo(Hershey) # examples of arrows > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >