Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using bold in mtext on string coming from vector element

Tags:

plot

r

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.

like image 311
Will Phillips Avatar asked Aug 16 '26 10:08

Will Phillips


1 Answers

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)
like image 116
Rorschach Avatar answered Aug 20 '26 17:08

Rorschach