Abby
Abby

Reputation: 59

Add line in textGrob

I would like to add a line to my textGrob. I am not sure how to go about it and if it is possible. This is the desired outcome:

Here is the code for the text part. Any help to achieve the first part will be appreciated.

textGrob("
A: Method One
B: Method Two ")

Desired outcome

Upvotes: 1

Views: 1190

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174478

You can't have a line as part of a pure textGrob unless you emulate it with underscores or long Unicode dashes ("\u2014"):

grid::grid.newpage()
tg <- grid::textGrob("\U2014\u2014 Observed median\nA: Method One\nB: Method Two",
                     x = 0.15,  hjust = 0, gp = gpar(cex = 4))
grid::grid.draw(tg)

enter image description here

If you want an actual line, simply add in a lineGrob. You can even combine a lineGrob and textGrob into a single grob:

tg <- grid::textGrob("        Observed median\nA: Method One\nB: Method Two",
                     x = 0.15,  hjust = 0, gp = gpar(cex = 4))
lg <- grid::linesGrob(unit(c(0.15, 0.28), "npc"), unit(c(0.63, 0.63), "npc"), 
                      gp = gpar(lwd = 8))
combo <- grid::grobTree(tg, lg)
grid::grid.newpage()
grid::grid.draw(combo)

enter image description here

This is probably closer to what you wanted, but the long dash hack is a bit easier.

Upvotes: 2

Related Questions