13

I am trying to get a label to have an exponent in it. Here's the code I have

vall = format(cor(x,y)*cor(x,y),digits=3)
eq <- expression(paste(R^2," = ",vall,sep=""))
text(legend.x,legend.y,eq,cex=1,font=2)

But the text simply looks like this enter image description here

How do I get the actual vall to show up (and not the text "vall")

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • 1
    duplicate ? http://stackoverflow.com/questions/4973898/combining-paste-and-expression-functions-in-plot-labels – user1317221_G Dec 28 '12 at 18:57
  • 1
    I don't think this is a duplicate but the title of this post is a bit misleading. May I suggest reformatting the title to reflect the problem as the exponent really doesn't seem to be your problem. – Tyler Rinker Dec 28 '12 at 19:01
  • `sep=""` does nothing here (appends a empty string). This is not `base::paste` but rather `plotmath`. Won't hurt here, but won't do what you want if a non-empty string is specified. – Matthew Lundberg Dec 28 '12 at 21:18

1 Answers1

21

Try bquote(), for example:

set.seed(1)
vall <- format(rnorm(1),digits=3)
eq <- bquote(bold(R^2 == .(vall)))
sq <- seq(0, 1, by = 0.1)
plot(sq, sq, type = "n")
text(0.5, 0.5, eq)

The reason your example doesn't work is that R never ends up evaluating vall:

> eq2 <- expression(paste(R^2," = ",vall,sep=""))
> eq2
expression(paste(R^2, " = ", vall, sep = ""))

plotmath tries to make something out of this but essentially vall is taken literally.

In general you don't need paste() in a plotmath expression, you can build the expression up using standard operators and through the use of layout operators. For example, for an expression equivalent to the one your example produced (unevaluated vall), all you really need is:

expression(R^2 == vall)

bquote() is one way to have an object replaced by its value in an expression. You wrap the object you want replaced by its value in .( ). R will then look for the object and takes its value and insert it into the expression.

See also substitute() for an alternative approach to this with a different interface.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • @CodeGuy Re `expression(R^2 == vall)` sorry if I wasn't clear; I meant that as a way to use `expression()` to get the same behaviour as your example gave (i.e. `vall` included *literally*). It was meant to illustrate that you don't need `paste()` for such things. `bquote()` or `substitute()` is required to have `vall` replaced by its value. – Gavin Simpson Dec 28 '12 at 20:11
  • What if you want to add some plaintext? I'm using an expression to simulate a subtitle in ggplot2 (see http://stackoverflow.com/a/14668651/210945), and I'd like to do something like `ggtitle(expression(atop(paste('Qg_obs vs Qg_sim for', model, 'at site', dataset), atop('colour represents angle from one point to the next', ''))`, but I can't find a way to get it to work. Have tried `.()` and bquote and substitute, but I can't figure it out. – naught101 Aug 29 '14 at 04:21