Search code examples
rr-grid

TextGrob R - fontface=bold but no bold


I'm trying to create a header for my graph pages with the text being bold. The code I'm using is below, I set fontface=bold but this doesn't seem to be working. Is there somewhere else I need to set it as well?

t = textGrob(expression(underline("My Sample Header")),gp=gpar(fontfamily="serif",fontsize=16, fontface="bold",lineheight=1),vjust=.3,hjust=2.15)

Solution

  • Since no one has answered this, you could do this two ways:

    Method 1: Use expressions but do expression(bold(underline(...))) like:

    t = textGrob(expression(bold(underline("My Sample Header"))),gp=gpar(fontfamily="serif",fontsize=16, fontface="bold",lineheight=1),vjust=.3,hjust=1)
    grid.draw(t)
    

    Method 2: Write a wrapper function which combines the textGrob with a line (probably a better option):

    underlineText <- function(text, gpar, vjust, hjust){
      txt <- textGrob(text, gp=gpar, vjust = vjust, hjust = hjust)
      undLine <- segmentsGrob(x0 = txt$x - grobWidth(txt), y0 = txt$y - unit(0.55, "grobheight", txt), x1 = txt$x, y1 = txt$y - unit(0.55, "grobheight", txt))
      gt <- grobTree(txt, undLine)
    }
    
    grid.draw(underlineText("My Sample Header", gpar(fontfamily="serif",fontsize=16, fontface="bold",lineheight=1), vjust = 0.3, hjust = 1))