Search code examples
rtextggplot2ggrepel

How do I include strikethrough text in geom_text_repel or geom_text labels for ggplot?


Is it possible to add strikethrough to some geom_text/geom_text_repel labels?

This question mentioned that you can italicise a label using the following:

library("ggplot2")
library("ggrepel")

df <- data.frame(
  x = c(1,2),
  y = c(2,4),
  lab = c("italic('Italic Text')", "Normal"))

ggplot(df, aes(x, y, label = lab)) +
    geom_point() +
    geom_text_repel(parse = T)

enter image description here

However, I've been unable to use the same method to get strikethrough text.

df$lab = c("strike('Strikethrough Text')", "Normal")

ggplot(df, aes(x, y, label = lab)) +
    geom_point() +
    geom_text_repel(parse = T)

enter image description here


Solution

  • What about using a Unicode long strike overlay?

    enter image description here

    R Script
    # Long strikethru test
    # Unicode Character 'COMBINING LONG STROKE OVERLAY' (U+0336)
    
    library("tidyverse")
    
    # Keep 30 first rows in the mtcars natively available dataset
    data <- head(mtcars, 30)
    
    name <- "Strikethrough"
    name_strk <- str_replace_all(name, "(?<=.)", "\u0336")
    
    # Add one annotation
    ggplot(data, aes(x=wt, y=mpg)) +
      geom_point() + # Show dots
      geom_label(
        label= name_strk,
        x=4.1,
        y=20,
        label.padding = unit(0.55, "lines"), # Rectangle size around label
        label.size = 0.35,
        color = "black",
        size = 4,
        fill="white"
      )