【问题标题】:How do I bold and underline part of a ggplot2 annotation?如何在 ggplot2 注释的部分加粗和下划线?
【发布时间】:2021-01-05 17:35:45
【问题描述】:

我正在尝试使用paste() 内的grDevices 中的bold()underline() 函数来创建一个注释,该注释具有一个风格化、硬编码的“标题”,其中一个换行符后跟一个可能是一个字符串或更多行,并且正在努力完成它。 (这是在 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)

非常感谢任何帮助。

【问题讨论】:

    标签: r ggplot2 annotate plotmath ggtext


    【解决方案1】:

    使用ggtext 包:

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

    【讨论】:

    • 但它不能下划线。可能是为了更好。下划线是打字机时代的遗物,通常应避免使用。
    猜你喜欢
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2014-08-30
    • 2023-04-04
    • 2015-09-13
    相关资源
    最近更新 更多