r - 如何在绘图标题或标签中的文本下划线?

标签 r ggplot2 underline plotmath

我似乎不知道如何在情节标题的任何部分下划线。

我能找到的最好的是 annotate("segment") done by hand ,并且我创建了一个玩具图来说明其方法。

df <- data.frame(x = 1:10, y = 1:10)

rngx <- 0.5 * range(df$x)[2]  # store mid-point of plot based on x-axis value
rngy <- 0.5 * range(df$y)[2]  # stores mid-point of y-axis for use in ggplot

ggplot(df, aes(x = x, y = y)) + 
  geom_point() +
  ggtitle("Oh how I wish for ..." ) +
  ggplot2::annotate("text", x = rngx, y = max(df$y) + 1, label = "underlining!", color = "red") +
  # create underline:
  ggplot2::annotate("segment", x = rngx-0.8, xend = rngx + 0.8, y= 10.1, yend=10.1)

enter image description here

uses bquote(underline() with base R

pertains to lines over and under nodes on a graph

uses plotmath and offers a workaround, but it didn't help

最佳答案

试试这个:

ggplot(df, aes(x = x, y = y)) + geom_point() +
  ggtitle(expression(paste("Oh how I wish for ", underline(underlining))))

或者,正如 BondedDust 在评论中指出的那样,您可以完全避免 paste() 调用,但要注意 for:

ggplot(df, aes(x = x, y = y)) + geom_point() +
  ggtitle(expression(Oh~how~I~wish~'for'~underline(underlining)))

或者巴蒂斯特建议的另一种更短的方法,不使用表达式paste()或许多波浪号:

ggplot(df, aes(x = x, y = y)) + geom_point() +
  ggtitle(~"Oh how I wish for "*underline(underlining))

关于r - 如何在绘图标题或标签中的文本下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30470293/

相关文章:

r - 光滑的2D表面

r - ggplot2 - 组合 geom_point 和 geom_line 的图例

ios - 如何在 UILabel 中制作带下划线的文本?

java - 如何使用eclipse在java中打印带下划线的字符串

r - 如何使用R精确匹配整个数据集中的两列值

r - Topicmodels 转置术语文档矩阵

r - 叠加密度图,ggplot2,R 3.5.1。 , 错误 : Aesthetics must be either length 1 or the same as the data

css - 如何在我的文本之后创建一条与容器宽度相同的线?

R 因子 - 时间序列转换不起作用

javascript - 如何在 Rmarkdown 文档中使用 JavaScript 添加下载按钮