如何在图表标题或标签中加下划线? (GGPLOT2)

时间:2015-05-26 22:50:19

标签: r plot ggplot2 underline plotmath

如果这是一个简单的问题,请原谅我的无知,但我似乎无法弄清楚如何强调情节标题的任何部分。我正在使用ggplot2

我能找到的最好的是 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

1 个答案:

答案 0 :(得分:9)

试试这个:

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

或者baptiste建议的另一种更短的方法,即不使用expressionpaste()或许多代字号:

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