rggplot2annotationsitalics

Make an annotation in ggplot with applying styles on the certain symbols


I am trying to put the r2 value and p-value in the same annotation in a graph.

The r and the p needs to be italicized with a comma in between the two values so r^2 = 0.26, p = 0.005.

I have looked up suggestions but I can't get everything I want. The first example code gets me everything but the italic p.

I have tried putting paste italic in front of it like I did with the r but it gives me an error and the second script the r and the p are italic but I can't figure out how to put a comma in between them

annotate("text", x=93, y=65, 
         label= "paste(italic(r)^{2}, \" = 0.26, \",p, \" = 0.0008\")", 
         parse=TRUE, size=4)

annotate("text", x=93, y=65, 
         label= "italic(r)^{2}==0.26~italic(p)==0.0008", 
         parse=TRUE, size=4)

I want the r and the p to be italic and I want a comma between them.


Solution

  • library(ggplot2)
    
    ggplot()+
      annotate("text", x=93, y=65, 
               label= "paste(italic(r)^{2}, \" = 0.26, \",italic(p), \" = 0.0008\")", 
               parse=TRUE, size=4) + 
      #setting axis to blank just to make this visually appealing:
      theme(axis.title.x=element_blank(),
            axis.text.x=element_blank(),
            axis.ticks.x=element_blank(),
            axis.title.y=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks.y=element_blank())