Will Phillips
Will Phillips

Reputation: 837

using bold in mtext on string coming from vector element

I've learned to apply bold to a portion of the text used in a plot title using mtext() expression() and paste(). This works great if you specify the strings outright. However, in the project I'm working on now, the portion of text to be bolded needs to be obtained through a call to an element of a vector. However, the characters needed in the call syntax are interpreted by expression() and the call fails.

junk <- c("I'm Special", "You're Special")
plot(0, type="n")
mtext(expression(paste("Do you think ", bold(junk[1]),"today?")),3,2)
mtext(expression(paste("I think ", bold(junk[2]), "today.")),3,1)

Any thoughts on how to approach this? I am trying to avoid specifying the bold text directly.

Upvotes: 3

Views: 2393

Answers (1)

Rorschach
Rorschach

Reputation: 32466

bquote has a decent interface for this. You just surround the variable you want to substitute with .(). You could also use substitute with expression.

junk <- c("I'm Special", "You're Special")
plot(0, type="n")
mtext(bquote(paste("Do you think ", bold(.(junk[1])),"today?")),3,2)
mtext(bquote(paste("I think ", bold(.(junk[2])), "today.")),3,1)

Upvotes: 4

Related Questions