4

I am trying to plot a title, a greek character and as well the mean of a variable. I want my plot title to look something like this (but centered)

Title

μ=1.2

I made a few attepmts:

d <- rnorm(100)
hist(d, main=expression(paste("Title\n", mu, "=", mean(d))))
hist(d, main=expression(paste("Title\n", mu, "=", mymean), list(mymean=mean(d))))
hist(d, main=paste(expression(paste("Title\n", mu, "="), mean(d))))
hist(d, main=expression(atop("Title", substitute(mu, "=", mymean, list(mymean=mean(d))))))
hist(d, main=expression("Title\n", substitute(mu, "=", mymean, list(mymean=mean(d)))))

but I don't know how to use expression or substitute correctly in the title. I know that mtext would be a possibility, but it should work using the main= argument...?

user1981275
  • 13,002
  • 8
  • 72
  • 101
  • 1
    i think the three requirements are incompatible, unless you use unicode for mu: `plot(1,1, main=paste("Title\n\U03BC=",1.2))` – baptiste Apr 11 '13 at 11:51
  • 2
    possible duplicate of [Plot main title in two lines](http://stackoverflow.com/questions/14136311/plot-main-title-in-two-lines) – baptiste Apr 11 '13 at 11:57
  • Looks similar, I must have overlooked this when I asked the question... – user1981275 Apr 11 '13 at 12:12

2 Answers2

9

You can do this with atop:

hist(d, main=bquote(atop(Title,mu==.(mean(d)))))

enter image description here

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
2

Use the plain() function to specify normal text and *, ~ and ~~ as a spacers:

mtext(main=bquote(plain("Observation at pH ") ~ .(pH) ~~ plain(" [") *  mu * plain("M]")))

This is explained somewhat cryptically in ?plain (which translates into help regarding plain, when typed in the R command line prompt). You will then get the help page Mathematical Annotation in R, then search for plain in that page.

Florian
  • 1,827
  • 4
  • 30
  • 62