Colleagues, I am encountering difficulty adding formatted text to a graphic. Specifically, I want to add a superscript in the middle of a text string but I would like to format the text string on the fly. The commands: plot(1,1) ARG <- bquote('TEXT'^'\u00ae') mtext(ARG, line=-2, side=1) yield the desired output. However, my goal is to paste together a string, then pass it to mtext. For example: plot(1,1) PART1 <- 'TEXT' PART2 <- '^' PART3 <- '\u00ae' ARG <- paste(PART1, PART2, PART3) mtext(bquote(.(ARG)), line=-2, side=1) ## bquote(ARG) also does not work This does not work -- the unprocessed string: TEXT ^ ? is printed. Obviously, I don't understand some aspect of passing arguments to bquote. Of note, I tried "expression" instead of bquote but I was not able to get the registered sign ('\u00ae') to appear as a superscript. Any help would be appreciated. Dennis (R 2.11; OS X [platform should not be an issue here]) Dennis Fisher MD P < (The "P Less Than" Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-866-PLessThan (1-866-753-7784) www.PLessThan.com
On Aug 1, 2010, at 11:03 AM, Dennis Fisher wrote:> Colleagues, > > I am encountering difficulty adding formatted text to a graphic. > Specifically, I want to add a superscript in the middle of a text > string but I would like to format the text string on the fly. The > commands: > plot(1,1) > ARG <- bquote('TEXT'^'\u00ae') > mtext(ARG, line=-2, side=1) > yield the desired output. > > However, my goal is to paste together a string, then pass it to > mtext. For example: > plot(1,1) > PART1 <- 'TEXT' > PART2 <- '^' > PART3 <- '\u00ae' > ARG <- paste(PART1, PART2, PART3) > mtext(bquote(.(ARG)), line=-2, side=1) ## bquote(ARG) also does > not work > This does not work -- the unprocessed string: > TEXT ^ ? > is printed. > > Obviously, I don't understand some aspect of passing arguments to > bquote. Of note, I tried "expression" instead of bquote but I was > not able to get the registered sign ('\u00ae') to appear as a > superscript.I'm would be embarrassed to state the number of failed attempts I made with substitute, expression,, as.expression and parse before eventual success, but here's what finally "worked". plot(1,1); mtext(bquote(.(PART1)^.(PART3)), line=-2, side=1) I think trying to substitute for "^" will require a different approach, since sticking a series of .() calls seems to be unacceptable to the interpreter : ## "Does not work" plot(1,1); mtext(bquote(.(PART1).(PART2).(PART3)), line=-2, side=1) #Error: unexpected symbol in " mtext(bquote(.(PART1)."> > Any help would be appreciated. > > Dennis > > (R 2.11; OS X [platform should not be an issue here])I cannot assess, since that is also my platform. -- David Winsemius, MD West Hartford, CT
On Sun, Aug 1, 2010 at 11:03 AM, Dennis Fisher <fisher at plessthan.com> wrote:> Colleagues, > > I am encountering difficulty adding formatted text to a graphic. ?Specifically, I want to add a superscript in the middle of a text string but I would like to format the text string on the fly. ?The commands: > ? ? ? ?plot(1,1) > ? ? ? ?ARG ? ? <- bquote('TEXT'^'\u00ae') > ? ? ? ?mtext(ARG, line=-2, side=1) > yield the desired output. > > However, my goal is to paste together a string, then pass it to mtext. ?For example: > ? ? ? ?plot(1,1) > ? ? ? ?PART1 ? <- 'TEXT' > ? ? ? ?PART2 ? <- '^' > ? ? ? ?PART3 ? <- '\u00ae' > ? ? ? ?ARG ? ? <- paste(PART1, PART2, PART3) > ? ? ? ?mtext(bquote(.(ARG)), line=-2, side=1) ? ? ? ? ?## bquote(ARG) also does not work > This does not work -- the unprocessed string: > ? ? ? ? TEXT ^ ? > is printed. > > Obviously, I don't understand some aspect of passing arguments to bquote. ?Of note, I tried "expression" instead of bquote but I was not able to get the registered sign ('\u00ae') to appear as a superscript. > > Any help would be appreciated.Try this (noting the backquotes around \u00ae since otherwise its not a valid R name): x <- "TEXT^`\u00ae`" plot(0, main = parse(text = x))
I failed many times until Gabor gave his solution. The magic tick! PART1 <- 'TEXT' PART2 <- '^' PART3 <- '`\u00ae`' ARG <- paste(PART1, PART2, PART3) text(2,8, parse(text=str)) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Constructing-arguments-for-plotmath-tp2309474p2309539.html Sent from the R help mailing list archive at Nabble.com.