7

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.

Community
  • 1
  • 1
user2040907
  • 73
  • 1
  • 5
  • no worries i see the issue more clearly with your edit, glad it may be useful in the future though – user1317221_G Feb 05 '13 at 16:54
  • I don't think it is possible. Different in-text formatting can be achieved using expressions (your first example using `text`). However, there is no version of `textplot` which takes expressions; there is only one that takes characters. – Brian Diggs Feb 05 '13 at 21:12
  • Is there any reason not to switch graphics packages? It sounds like you're doing an lot of custom designing, so the base 'grid' package may be worth your time. You can see a general intro to 'grid' here - http://stackoverflow.com/questions/13081310/combining-multiple-complex-plots-as-panels-in-a-single-figure/14567298#14567298 – Dinre Feb 12 '13 at 12:51
  • @BrianDiggs It seems that `textplot.character` method uses a call to `text` for the plotting, so you can alter the function to pass it an expression rather than the default character object to render text using `expression`. – Simon O'Hanlon Feb 19 '13 at 14:10
  • I am curious to know if my answer below helped you with your use-case? Many thanks, Simon – Simon O'Hanlon Feb 23 '13 at 14:30

1 Answers1

3

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.

# Pass the function a string and a character to split on
# The splitting is greedy (i.e. it will split on all matches so make sure you are splitting on a unqiue character such as ":" in your example)
tsplit <- function( string , split ){
    require( stringr )
    blurb <- paste( string )
    blurbs <- strsplit( blurb , paste(split) )
    annot <- bquote( paste( bold( .( blurbs[[1]][1] ) ) , .(split) , .(blurbs[[1]][2]) , sep = "" ) )
    return( annot )
}



#And the function in action...
j <- tsplit( "Define: This is my blurb" , ":" )
textplot( paste( " " ) ) #Get new plot
text(0.5 , 0.5 , j ) #paste the text

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 to text to 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 to textplot.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.

tplot.cust <-   function ( object , split , halign = c("center", "left", "right"), valign = c("center", 
"top", "bottom"), cex, fixed.width = TRUE, cspace = 1, lspace = 1, 
mar = c(0, 0, 3, 0) + 0.1, tab.width = 8, ...) 
{
# extra code to split text according to 'split' argument and make text before the split bold.
require(stringr)
blurb <- paste( object )
blurbs <- strsplit( blurb , paste(split) )
annot <- bquote( paste( bold( .( blurbs[[1]][1] ) ) , .(split) , .(blurbs[[1]][2]) , sep = "" ) )


object <- paste(object, collapse = "\n", sep = "")
object <- gplots:::replaceTabs(object, width = tab.width) #you need to add gplots::: to this line because replaceTabs is a function that is not exported from the gplots namespace
halign = match.arg(halign)
valign = match.arg(valign)
plot.new()
opar <- par()[c("mar", "xpd", "cex", "family")]
on.exit(par(opar))
par(mar = mar, xpd = FALSE)
if (fixed.width) 
    par(family = "mono")
plot.window(xlim = c(0, 1), ylim = c(0, 1), log = "", asp = NA)
slist <- unlist(lapply(object, function(x) strsplit(x, "\n")))
slist <- lapply(slist, function(x) unlist(strsplit(x, "")))
slen <- sapply(slist, length)
slines <- length(slist)
if (missing(cex)) {
    lastloop <- FALSE
    cex <- 1
}
else lastloop <- TRUE
for (i in 1:20) {
    oldcex <- cex
    cwidth <- max(sapply(unlist(slist), strwidth, cex = cex)) * 
        cspace
    cheight <- max(sapply(unlist(slist), strheight, cex = cex)) * 
        (lspace + 0.5)
    width <- strwidth(object, cex = cex)
    height <- strheight(object, cex = cex)
    if (lastloop) 
        break
    cex <- cex/max(width, height)
    if (abs(oldcex - cex) < 0.001) {
        lastloop <- TRUE
    }
}
if (halign == "left") 
    xpos <- 0
else if (halign == "center") 
    xpos <- 0 + (1 - width)/2
else xpos <- 0 + (1 - width)
if (valign == "top") 
    ypos <- 1
else if (valign == "center") 
    ypos <- 1 - (1 - height)/2
else ypos <- 1 - (1 - height)
text(x = xpos, y = ypos, labels = annot , adj = c(0, 1), 
    cex = cex, ...) #add the newly created annot expression here
par(opar)
invisible(cex)
}

We can then use tplot.cust like so...

blurb <- "Define: This is my blurb"
tplot.cust(blurb, ":" , halign="left", valign="top", cex = 1,  family="serif")

Hopefully this is what you want??

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184