1

I want include a variable in mtext as well as superscript some text:

My ideal output is

y.label ADC (10^-6 mm^2/s) 

where y.label is the value of a variable, -6 is superscripted and 2 is superscripted

The most promising approach so far is:

mtext(expression(paste(y.label, " ADC (", 10^-6, " ", mm^2, "/s)")), side=2, line=2.5, cex=1.2, font=1)

This almost works but outputs "y.label" instead of the value of y.label

Grateful for any suggestions.

lajulajay
  • 355
  • 3
  • 4
  • 18

1 Answers1

3

This should work:

y.label <- 1
plot(1:10,1:10, type = "n", ylab = "")
gaa <- parse(text=paste(y.label, "*~ADC(10^-6*~mm^2/s)"))
mtext(gaa, side=2, line=2.5, cex=1.2, font=1)

enter image description here

For explanation see here

EDIT parse freaks out because of the % sign. You could use substitute instead. Here you have to pass y.label into the function through a list/environment. See the help sheet.

y.label <- "PVP 10%"
gaa <- substitute(paste(N, " ADC (", 10^-6, " ", mm^2, "/s)"), list(N=y.label))
plot(1:10,1:10, type = "n", ylab = "")
mtext(gaa, side=2, line=2.5, cex=1.2, font=1)

enter image description here

Community
  • 1
  • 1
Mikko
  • 7,530
  • 8
  • 55
  • 92
  • Almost there: I am using this in a loop and one of the values of y.label is the string **"PVP 10%"**. For some reason, that string triggers an error: `unexpected numeric constant`. I'll keep at it but please let me know if you have any ideas why. Your explanation link is fantastic BTW. – lajulajay Apr 08 '14 at 21:27