0

I have trying several options to add significance Levels as text labels to a plot. I got the result using geom_richtext() in the ggtext package. Although the labels turn out fine, I am getting the code pasted on the graph/ plot. Any idea how to avoid that! Or is there any other option?

geom_richtext(x = 1.15, 
                            y = 0.2, 
                            label.color = "black",
                            label = "<b>Significance Levels</b><br>ns <i>p</i> > 0.05<br>* <i>p</i> <= 0.05<br>** <i>p</i> <= 0.01<br>*** <i>p</i> <= 0.001<br>**** <i>p</i> <= 0.0001")[enter image description here][1]
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 13 '21 at 00:02
  • I was not allowed to post the graph since I am not an experienced user. It's just a label for the plot. Nothing connected to the data. – rottweiller Oct 13 '21 at 10:21

1 Answers1

1

Try this:

library(gridtext)
library(ggplot2)

label <- "<b>Significance Levels</b><br>ns <i>p</i> > 0.05<br>* <i>p</i> <= 0.05<br>** <i>p</i> <= 0.01<br>*** <i>p</i> <= 0.001<br>**** <i>p</i> <= 0.0001"

ggplot(BOD, aes(Time, demand)) +
  geom_point() + 
  annotation_custom(richtext_grob(label), ymin = 0.2)

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks a ton :) – rottweiller Oct 13 '21 at 10:25
  • for some reason this doesn't work for me. Even if I open a clean R session, and copy and paste your code, the font all looks the same on the plot (the breaks work, but the bold and italics formatting don't apply). Any guesses why? Thanks – yt1300 May 24 '22 at 06:56
  • I get the graph shown with ggplot2 3.3.6, gridtext 0.1.4 and "R version 4.1.3 Patched (2022-03-10 r81883)" under "Windows 10 x64 (build 19044)". What versions and platform do you have? `packageVersion("ggplot2"); packageVersion("gridtext"); R.version.string; win.version()` – G. Grothendieck May 24 '22 at 13:54