Koen
2009-Dec-11 12:46 UTC
[R] Array of legend text with math symbols from predefined variables
Hello, I am trying to include legend text with math symbols from a predefined character variable that is read in from a file. ? If there is only one line of text in the legend, the following, although cumbersome, works for me: ? > LegendText = " 'U' [infinity], '=10 m/s' "?? # (read in from a file) ??> LegendName = paste("bquote(paste(",LegendText, "))") ? > plot( c(1,2,3), c(1,2,3) ) ? > legend(?topleft?,1, eval(parse(text=LegendName)) ) ?If I now have more than one line in a plot and hence want to include more than one line of legendtext, I run into problems because ? > legend(?topleft?,1, c( ?eval(parse(text=LegendName1)),eval(parse(text=LegendName2)) ?) will not result in evaluation of the character string but just a paste of the string and ? > legend(?topleft?,1, eval(parse(text=c(LegendName1, LegendName2))) ?) will only evaluate the first element of the array. Am I trying to do the impossible or is my approach totally wrong? Any help would be greatly appreciated! Koen
Uwe Ligges
2009-Dec-13 18:36 UTC
[R] Array of legend text with math symbols from predefined variables
See Ligges, U. (2002): R Help Desk: Automation of Mathematical Annotation in Plots. R News 2 (3), 32-34. http://www.r-project.org/doc/Rnews/Rnews_2002-3.pdf Uwe Ligges Koen wrote:> Hello, > > I am trying to include legend text with math symbols from a predefined > character variable that is read in from a file. > > If there is only one line of text in the legend, the following, although > cumbersome, works for me: > > > LegendText = " 'U' [infinity], '=10 m/s' " # (read in from a file) > > LegendName = paste("bquote(paste(",LegendText, "))") > > plot( c(1,2,3), c(1,2,3) ) > > legend(?topleft?,1, eval(parse(text=LegendName)) ) > > If I now have more than one line in a plot and hence want to include more > than one line of legendtext, I run into problems because > > > legend(?topleft?,1, c( > eval(parse(text=LegendName1)),eval(parse(text=LegendName2)) ) > > will not result in evaluation of the character string but just a paste of > the string and > > > legend(?topleft?,1, eval(parse(text=c(LegendName1, LegendName2))) ) > > will only evaluate the first element of the array. > > Am I trying to do the impossible or is my approach totally wrong? Any help > would be greatly appreciated! > > Koen > > ______________________________________________ > R-help at r-project.org 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.