I’m using textplot() from the gplots package to write definitions that are then displayed next to other plots using par(mfrow=c(3,2)).
I want to change a single word in the character string to bold face (Usually the word being defined). Is there a metacharacter that will let me do this inside of the ” “? Or another solution for picking out words and giving them bold attributes without assigning that to the whole string?
It’s similar to this question, but I wasn’t able to use the same technique in textplot():
text() R-function – how to change the font of a single word?
text(0.5,0.5, expression(paste(bold("bold")," not bold")))
Here’s my code without a bolded term. Pretend “Definition” is desired to be bold face:
blurb<-strwrap("Definition: This is my text blurb",
width=60)
textplot(blurb, halign="left", valign="top", cex = 1, family="serif")
I’ve been playing with breaking the string apart and searching for a function that will assign bold face to the “Definition” portion, font=2, and then pasting the string back together, but I’m stumped. I can’t find a function to use:
blurb1<-"Definition" ##How to change to bold face??
blurb2<-"This is my text blurb"
blurb<-paste0(blurb1,blurb2)
EDIT: The predominant barrier to using other solutions is that for my page layout, text() isn’t entirely viable. I’m hoping to find a solution to editing the string either inside of textplot() or in a way that can be passed to textplot().
I’m creating something of a “Report Card” that will plot user data and provide a paragraph of explanation beside the plot. Different values would trigger a different textplot(). I like textplot() because it’s easily placed with par(mfrow=c(4,2)), carving out a seperate space without overlapping other plots. I just can’t seem to work text() in without a lot of play in the positioning.
You need to use
bquote().Here is a simple function which takes a text string and splits it and returns the appropriate expression for your bold plotting needs. I am sure you can adapt this as you see fit.
I hope this helps. The function assumes that there is only one unique character to split the string on and that you want the first word in bold and the rest of the string in normal format.
Cheers
EDIT
Sorry I realised in the question you said you couldn’t use text for placement because it is problematic. A quick check of the available methods of textplot (
showMethods(textplot)) and the source of the apporopriate method for plotting characters (getAnywhere(textplot.character)) shows that textplot does infact use a call totextto annotate the plot with your text object. Most of the code is concerned with taking out the heavy lifting of where you want the text. You can make a couple of simple adjustments totextplot.character()to create a custom function to do what you wanted. You can copy and paste this into R and it should work as per the example at the bottom.We can then use tplot.cust like so…
Hopefully this is what you want??