Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9306587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:12:22+00:00 2026-06-19T00:12:22+00:00

I’m using textplot() from the gplots package to write definitions that are then displayed

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:12:23+00:00Added an answer on June 19, 2026 at 12:12 am

    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??

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I am using jsonparser to parse data and images obtained from json response. When
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.