[R] How to vectorize
Dear R-List, I would like to write nicely the names of some isotopes on a plot. The code bellow works fine. plot(1:10,1:10) text(c(2,4,8),c(2,4,8),labels=c(expression(italic(phantom(0)^{78}*Ge)), expression(italic(phantom(0)^{137}*Cs)), expression(italic(phantom(0)^{129*m}*Te))), cex=3 ) But, since I have a lot of isotopes to write on the plot, I would like to construct automatically the labels. So I wrote the code below which works fine. listenoms <- list(nom=c("Ge","Cs","Te"),num=c("78","137","129*m")) n <- length(listenoms$nom) resu <- "c(" for( i in 1:(n-1)) { resu <- paste(resu,paste("expression(italic(phantom(0)^{", listenoms$num[i],"}*", listenoms$nom[i],")),",sep="")) } resu <- paste(resu,paste("expression(italic(phantom(0)^{", listenoms$num[n],"}*", listenoms$nom[n],")))",sep="")) plot(1:10,1:10) text(c(2,4,8),c(2,4,8),labels=eval(parse(text=resu)),cex=2) I assume there is a better way to do that using vectorization. May you help me to find it ? Thanks Laurent __ R-help@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
[R] How to use expression in label with xYplot
Dear R-List, I want to use the label function (from Hmisc library) to allow for the names of my isotopes. library(Hmisc) library(lattice) library(grid) num <- c("78","137","129m") nom <- c("Ge","Cs","Te") df <- data.frame(GE78=seq(nom),CS137=seq(nom),TE129m=seq(nom)) if I use this function to create the labels : lab <- function(i) as.expression(bquote(italic(phantom(0)^{.(num[i])}*.(nom[i] label(df$GE78) <- lab(1) label(df$CS137) <- lab(2) label(df$TE129m) <- lab(3) all works fine when I use text and xyplot : plot(1:10) text(6,6,labels=label(df$CS137)) xyplot(CS137~TE129m,data=df,xlab=label(df$CS137),ylab=label(df$TE129m)) but xYplot doesn't work fine xYplot(CS137~TE129m,data=df) I have the message : Error in parse(file, n, text, prompt) : parse error if I change the lab function lab <- function(i) as.character(paste("italic(phantom(0)^{\"",num[i],"\"}*\"",nom[i],"\")",sep= "")) text and xyplot work fine if I use the parse function and xYplot works fine. label(df$GE78) <- lab(1) label(df$CS137) <- lab(2) label(df$TE129m) <- lab(3) plot(1:10) text(6,6,labels=parse(text=label(df$CS137))) xyplot(CS137~TE129m,data=df,xlab=parse(text=label(df$CS137)),ylab=parse(text =label(df$TE129m))) xYplot(CS137~TE129m,data=df) What is the good way to use expression in the labels of variables with the label function ? thanks __ R-help@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