[R] how to plot density distribution with a arrow pointer?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 17 04:55:45 CEST 2005


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




More information about the R-help mailing list