[R] stars graphs

Tito de Morais Luis Luis.Tito-de-Morais at ird.sn
Thu Aug 14 17:22:49 CEST 2003


Jim Lemon replied directly to me with a helpful function that needs to
be adapted. Yet it almost does exactly what I want. I'm posting it so
that it appears in the thread as it could be useful to others.

Thank you Jim

Tito


Le jeu 14/08/2003 à 15:28, Jim Lemon a écrit :
> If I understand this, it is fairly easy to write a function to do this 
> plot. Here is a very basic example. You will probably want to customize 
> this to suit your needs, and the plotting of text labels is _very_ basic. 
> Hope it helps.
> 
> Jim
> 
> # this utility just rescales data to a new range
> rescale<-function(x,newrange) {
>  if(nargs() > 1 && is.numeric(x) && is.numeric(newrange)) {
>   # if newrange has max first, reverse it
>   if(newrange[1] > newrange[2]) {
>    newmin<-newrange[2]
>    newrange[2]<-newrange[1]
>    newrange[1]<-newmin
>   }
>   xrange<-range(x)
>   if(xrange[1] == xrange[2]) stop("can't rescale a constant vector!")
>   mfac<-(newrange[2]-newrange[1])/(xrange[2]-xrange[1])
>   invisible(newrange[1]+(x-xrange[1])*mfac)
>  }
>  else {
>   cat("Usage: rescale(x,newrange)\n")
>   cat("\twhere x is a numeric object and newrange is the min and max of 
> the new range\n")
>  }
> }
> 
> star.plot<-function(lengths,rad.pos,labels) {
>  maxlength<-max(lengths)+0.1*!missing(labels)
>  plot(c(-maxlength,maxlength),c(-maxlength,maxlength),type="n",axes=FALSE,
>   xlab="",ylab="")
>  npos<-length(rad.pos)
>  # add one space to prevent overlapping
>  newrange<-c(0,2*pi*npos/(npos+1))
>  # rescale to a range of 0 to almost 2pi
>  rad.pos<-rescale(rad.pos,newrange)
>  # get the vector of x positions
>  xpos<-cos(rad.pos)*lengths
>  # get the vector of y positions
>  ypos<-sin(rad.pos)*lengths
>  segments(0,0,xpos,ypos)
>  if(!missing(labels)) {
>   xpos<-xpos*1.1
>   ypos<-ypos*1.1
>   text(xpos,ypos,labels)
>  }
> }
-- 
L. Tito de Morais
      UR RAP
   IRD de Dakar
      BP 1386
       Dakar
      Sénégal

Tél.: + 221 849 33 31
Fax: +221 832 16 75
Courriel: tito at ird.sn




More information about the R-help mailing list