TextGrob R - fontface =粗体但没有粗体

时间:2017-08-27 20:52:36

标签: r r-grid

我正在尝试为图表页面创建标题,文字为粗体。我正在使用的代码如下,我设置fontface = bold但这似乎不起作用。 还有其他地方我需要设置它吗?

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

1 个答案:

答案 0 :(得分:2)

由于没有人回答这个问题,你可以通过两种方式做到这一点:

方法1:使用表达式但expression(bold(underline(...)))如:

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)

方法2:编写一个包装函数,它将textGrob与一条线组合在一起(可能是更好的选择):

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