How to underline the text in the title or label? (Ggplot2)

Please forgive my ignorance if this is a simple question, but I canโ€™t understand how to emphasize any part of the title of the plot. I am using ggplot2 .

The best I could find was to annotate (a โ€œsegmentโ€) by hand , and I created a toy story to illustrate its method.

 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 R base

refers to the lines above and below the nodes in the graph

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

+7
r plot ggplot2 underline plotmath
source share
1 answer

Try the following:

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

Alternatively, as BondedDust points out in the comments, you can completely avoid calling paste() , but don't look at for :

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

Or another, even shorter, approach suggested by the Baptist who does not use expression , paste() or many tildes:

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

All Articles