MrJ Man
2006-Dec-20 23:19 UTC
[R] How to use strings from a data.frame as the argument of an expression() for plot
Greetings, I would like to use a data.frame with strings to feed the expression() in the title of a plot. The way I did this is: molecules <-data.frame(name=c("o3","no","no2"),expression=c("quote(O[3])","quote(NO)","quote(NO[2])")) for (mol in c(5,7,9)) { plot(x, y, type="b", main=eval(substitute(expression(paste(mol," Year 2005")), list(mol=eval(parse("",text=toString(molecules$expression[(mol-3)/2]))))))) } However, this looks cumbersome and I'm sure there is a way to do this in R that is much more simple. The complexity of the expression above is mainly due to the fact that the only way I could find to convert a string read from a data.frame into a symbol was to enclose it in "quote(symbol)" and call toString on the result, since selecting string data from a data.frame returns an object that is not a string (why is this so? A data.frame with doubles returns doubles). What do you think? Thanks in advance.
Gabor Grothendieck
2006-Dec-20 23:34 UTC
[R] How to use strings from a data.frame as the argument of an expression() for plot
Try this: e <- expression(O[3], NO, NO[2]) opar <- par(mfrow = c(2,2)) for(i in 1:3) plot(1, 1, type = "b", main = bquote(.(e[[i]]) ~ Year ~ 2005)) par(opar) Also please read the last line to every post to r-help and particularly note the part about reproducible examples. x and y.were undefined. On 12/20/06, MrJ Man <auxsvr at yahoo.com> wrote:> Greetings, > > I would like to use a data.frame with strings to feed > the expression() in the title of a plot. The way I did > this is: > > molecules > <-data.frame(name=c("o3","no","no2"),expression=c("quote(O[3])","quote(NO)","quote(NO[2])")) > > for (mol in c(5,7,9)) { > plot(x, y, type="b", > main=eval(substitute(expression(paste(mol," Year > 2005")), > > list(mol=eval(parse("",text=toString(molecules$expression[(mol-3)/2]))))))) > } > > However, this looks cumbersome and I'm sure there is a > way to do this in R that is much more simple. The > complexity of the expression above is mainly due to > the fact that the only way I could find to convert a > string read from a data.frame into a symbol was to > enclose it in "quote(symbol)" and call toString on the > result, since selecting string data from a data.frame > returns an object that is not a string (why is this > so? A data.frame with doubles returns doubles). > > What do you think? > > Thanks in advance. > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code. >
MrJ Man
2006-Dec-21 00:31 UTC
[R] How to use strings from a data.frame as the argument of an expression() for plot
Thanks for your response; one more thing: Is it possible to use a data.frame for the same effect, as these values are associated with some others (I could of course use them separately, but merging them in a data.frame should be possible in R)?> Try this: > > > e <- expression(O[3], NO, NO[2]) > opar <- par(mfrow = c(2,2)) > for(i in 1:3) plot(1, 1, type = "b", main bquote(.(e[[i]]) ~ Year ~ > 2005)) par(opar) > > Also please read the last line to every post tor-help and particularly> note the part about reproducible examples. x andy.were undefined.>
Gabor Grothendieck
2006-Dec-21 02:19 UTC
[R] How to use strings from a data.frame as the argument of an expression() for plot
Just store them as strings in the data frame and then perform a conversion to expressions prior to the loop: DF <- data.frame(s = c("O[3]", "NO", "NO[2]"), stringsAsFactors = FALSE) e <- parse(text = DF$s) ... continue with prior solution ... On 12/20/06, MrJ Man <auxsvr at yahoo.com> wrote:> Thanks for your response; one more thing: Is it > possible to use a data.frame > for the same effect, as these values are associated > with some others (I could > of course use them separately, but merging them in a > data.frame should be > possible in R)? > > > Try this: > > > > > > e <- expression(O[3], NO, NO[2]) > > opar <- par(mfrow = c(2,2)) > > for(i in 1:3) plot(1, 1, type = "b", main > bquote(.(e[[i]]) ~ Year ~ > > 2005)) par(opar) > > > > Also please read the last line to every post to > r-help and particularly > > note the part about reproducible examples. x and > y.were undefined. > > > > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com >