如何加粗和加下划线ggplot2注释的一部分?

问题描述

我正在尝试使用bold()underline()中的Grdevicespaste()函数来创建一个注释,该注释具有带换行符的风格化,硬编码的“标题”然后是一个可能长达一行或更多行的字符串,并且正在努力实现。 (这是在ShinyApp中完成的,因此我无法对两个相邻的注释进行硬编码,因为字符串中的行数会根据用户的输入而有所不同。)

library(ggplot2)
library(Grdevices)

mydata <- data.frame(Strings = c("This is a list of strings","They Could be \n one line long","Or they Could \n be several lines \n long"),NumberOfLines = c(1,2,3))

rowposition <- sample(1:3,1)

mystring <- mydata$Strings[rowposition]

emptydataframe <- data.frame()

ggplot(emptydataframe) +
  geom_blank() +
  annotate("text",x = 8,y = -4,label = paste(bold(underline("Title\n")),mystring),size = 3)

The plot that is returned contains "Title" wrapped in alphanumeric gibberish followed by the string.

非常感谢您的帮助。

解决方法

使用ggtext软件包:

library(ggtext)
ggplot(emptydataframe) +
  geom_blank() +
  annotate("richtext",x = 8,y = -4,label = paste("<b>Title</b><br>",mystring),size = 3)

enter image description here