Search code examples
rggplot2annotationsdivision

How do I annotate a division with the numerator and denominator vertically aligned and separated by a horizontal line


I'm currently trying to add an annotation to my ggplot which displays the fraction 1/9 in the specific format shown in the image I've provided. Despite it seeming like a straightforward task, I find myself stuck and unable to figure out the correct way to accomplish this. Any guidance on how to achieve this would be greatly appreciated.

enter image description here

Here is an example:

ggplot(mtcars, aes(cyl, mpg)) +
  geom_point()+
  annotate("text", x = 3, y = 3, label = "1/9", size = 10, color = "red") +
  coord_cartesian()

enter image description here


Solution

  • One option to add a fraction would be to use ?plotmath, i.e. to create your fraction you could use label = "frac(1, 9)" and set parse=TRUE in annotate():

    library(ggplot2)
    
    ggplot(mtcars, aes(cyl, mpg)) +
      geom_point() +
      annotate("text",
        x = 5, y = 30, label = "frac(1, 9)", size = 10, color = "red",
        parse = TRUE
      ) +
      coord_cartesian()