Search code examples
rggplot2annotationssuperscript

Add an aditional letter to R squared for annotate in ggplot


I guess it is a very easy question, but I am not able to solve the problem. I am fitting a linear mixed effect model and I would like to insert the marginal and the conditional R squared in my plot. For that reasons I want to to label them:

R²m = 0.65

R²c = 0.75

With that code I get the R²:

annotate(geom = 'text', label = paste("R^2 == ", 0.65), parse = TRUE)

However, I tried to play around with that code but I was not able to get the letter behind my R². Do you have any suggestions?


Solution

  • One way could be to use bquote instead. This will add the "m" and "c" as an index to "R". I find it always a bit fiddly to use expressions in plots, especially if you want to get very fancy.

    library(ggplot2)
    ggplot(mtcars, aes(mpg, disp)) +
      geom_point()+
      annotate(geom = 'text', x = 20, y = 300, 
               label = bquote("R[m]^2 == 0.65"), parse = TRUE)
    

    Created on 2021-06-07 by the reprex package (v2.0.0)