[R] triax.plot: control legend position and size of point labels

Jim Lemon jim at bitwrit.com.au
Thu Dec 20 11:50:06 CET 2012


On 12/19/2012 11:18 PM, maxbre wrote:
> Given this example
>
> library(plotrix)
>
> a<-c(34,10,70)
> b<-c(33,10,20)
> c<-c(33,80,10)
>
> test<-data.frame(A=a,B=b,C=c)
>
> triax.plot(test,
>             main ="title",
>             at=seq(0.25,0.75,by=0.25),
>             tick.labels=list(l=seq(0.25,0.75,by=0.25),
>                             r=seq(0.25,0.75,by=0.25),
>                             b=seq(0.25,0.75,by=0.25)),
>             align.labels=TRUE,
>             show.grid=TRUE,
>             cc.axes=TRUE,
>             show.legend=TRUE,
>             label.points=TRUE,
>             point.labels=c("case 1","case 2", "case 3"),
>             col.symbols=c("red","blue","green"),
>             cex.ticks=0.8,
>             cex.axis=0.8,
>             lty.grid=2,
>             pch=17
>             )
>
> I would like to control the position of the legend (to be moved to a
> different place) and the size of point labels (to be reduced)
>
> I’ve been trying to work out the solution with par() but without much
> success, any help for this?
>
Hi maxbre,
For the legend, I would suggest calling triax.plot  with legend=FALSE 
and then adding the legend where you want it. To change the size of the 
point labels you would have to modify the function. Here is the 
triax.points function with the necessary modification:

triax.points<-function(x,show.legend=FALSE,label.points=FALSE,
  point.labels=NULL,col.symbols=par("fg"),pch=par("pch"),
  bg.symbols=par("bg"),cc.axes=FALSE,...) {

  if(dev.cur() == 1)
   stop("Cannot add points unless the triax.frame has been drawn")
  if(missing(x))
   stop("Usage: triax.points(x,...)\n\twhere x is a 3 column array of 
proportions or percentages")
  if(!is.matrix(x) && !is.data.frame(x))
   stop("x must be a matrix or data frame with at least 3 columns and 
one row.")
  if(any(x > 1) || any(x < 0)) {
   if(any(x < 0))
    stop("All proportions must be between zero and one.")
   if(any(x > 100))
    stop("All percentages must be between zero and 100.")
   # convert percentages to proportions
   x<-x/100
  }
  if(any(abs(rowSums(x)-1) > 0.01))
   warning("At least one set of proportions does not equal one.")
  sin60<-sin(pi/3)
  if(cc.axes) {
   ypos<-x[,3]*sin60
   xpos<-x[,1]+x[,3]*0.5
  }
  else {
   ypos<-x[,3]*sin60
   xpos<-1-(x[,1]+x[,3]*0.5)
  }
  nobs<-dim(x)[1]
  points(x=xpos,y=ypos,pch=pch,col=col.symbols,bg=bg.symbols,type="p",...)
  if(is.null(point.labels)) point.labels<-rownames(x)
  if(label.points) 
thigmophobe.labels(xpos,ypos,point.labels,cex=par("cex.axis"))
  if(show.legend) {
   legend(0.2,0.7,legend=point.labels,pch=pch,col=col.symbols,
    xjust=1,yjust=0)
  }
  invisible(list(x=xpos,y=ypos))
}

Note that some lines above may have been broken by the email client. If 
so, stitch them back together with a text editor before trying to 
"source" the file. I will probably add something like this to triax.plot 
in the next version of plotrix.

Jim




More information about the R-help mailing list