[R] text(x,y,greek character)

Marc Schwartz MSchwartz at mn.rr.com
Mon Oct 10 15:26:58 CEST 2005


On Mon, 2005-10-10 at 07:59 -0400, Roy Little wrote:
> Dear list,
> I would like to plot points with two types of labels, one at the data 
> point (the name of the point) and another offset a bit with another 
> factor which is either of the two greek characters alpha or beta. I have 
> tried to get the routine to plot a greek character with expression() or 
> with substitute() and have not yet had any success.  The following only 
> plots the word in english in plain text. Here is my subroutine and data:
> 
> ---------------------------------------------------
> vmat<-as.matrix(read.table("vmat"))
> Xm<-vmat[1:22,1:20]
> hemd<-read.table("threehem",header=T)
> Ym<-as.matrix(hemd[,2])
> gvdw.pls<-plsr(Ym ~ Xm,6,method="kernelpls")
> rsltv<-predict(gvdw.pls,comps=6)
> plot(Ym,rsltv,type="n",xlab="Actividad + 
> BiolÃ³gica",xlim=c(4.6,6),ylim=c(4.8,6),ylab=" Act. + 
> Biol.(Pred.)",main="QSAR LigaciÃ³n de Derivados de la Artemisina con + 
> Hemina",sub="Descriptores de Coeficientes VdW")
> 
> text(Ym,rsltv,labels=threehem$cpd)
> text(Ym,rsltv,labels=hemd$type,adj=c(0,-1))
> ---------------------------------------------

<snip of data>

I believe I have a solution for you, but you may want to consider the
presentation, as it gets a bit busy. Perhaps consider using two colors
for the numeric text, where each color represents either alpha or beta
and then indicate this in a legend.

This could be done using:

cols <- ifelse(hemd$type == "alpha", "red", "blue")
text(Ym,rsltv,labels=hemd$cpd, col = cols)
legend("topleft", 
       legend = c(expression(alpha), expression(beta)), 
       fill = c("red", "blue"))


Also, two notes:

1. If you are going to use a function that is not in the base R
distribution, please indicate this so that folks can help without having
to search. In this case, the plsr() function is in the pls package,
which required a library(pls) before using your code.


2. The line: 

  text(Ym,rsltv,labels=threehem$cpd)

should be:

  text(Ym,rsltv,labels=hemd$cpd)


Here is a solution:

   greek <- parse(text = as.character(hemd$type))

   text(Ym, rsltv, labels = greek, adj=c(0, -1))

hemd$type is a factor, so it needs to be converted to a character vector
before being able to be used as an expression. Using parse() then
converts the character vector to an expression. The result of the first
line is:

> greek
expression(alpha, beta, alpha, beta, beta, beta, beta, beta, 
    beta, beta, alpha, alpha, alpha, alpha, alpha, alpha, alpha, 
    alpha, beta, beta, alpha, alpha)

Thus, 'greek' can be used in text() as an expression.

HTH,

Marc Schwartz




More information about the R-help mailing list