user1981275
user1981275

Reputation: 13372

bquote in mtext for two variables

I would like to plot a histogram of some data, add a title and the mean and standard deviation of the data. I use mtext and bquote:

a <- rnorm(100)
hist(a, main="A")
mtext(bquote(mu==.(round(mean(a)), sigma==.(sd(a)))))

However, only mu is plotted and it seems I don't entirely understand bquote. How could I plot mu and sigma next to or on top of each other?

Upvotes: 1

Views: 1517

Answers (1)

agstudy
agstudy

Reputation: 121568

You want a combination of bquote() and some plotmath symbols.

a <- rnorm(100)
hist(a, main="A")
mean.a <- round(mean(a))
sd.a <- round(sd(a))
mtext(bquote(mu== ~.(mean.a) ~ sigma== ~.(sd.a)))

enter image description here

EDIT

If you want yo put on top of each other, since plotmath does not support newlines, you can create your lines one by one like this:

Lines <- list(bquote(mu== ~.(mean.a)),
              bquote(sigma== ~.(sd.a)))
mtext(do.call(expression, Lines),side=3,line=0.5:-0.5)

enter image description here

Upvotes: 3

Related Questions