How do I include a superscript to texts on a plot on R?

后端 未结 3 483
再見小時候
再見小時候 2020-12-17 16:33

I need it to look like this:

R^2 = some values

And I\'ve tried the code below but it wouldn\'t work, it came out as \"R (expression (^2)) = some values\" i

3条回答
  •  旧时难觅i
    2020-12-17 16:50

    How to include formatting and mathematical values in plots is FAQ 7.13.

    For example, if ahat is an estimator of your parameter a of interest, use

    title(substitute(hat(a) == ahat, list(ahat = ahat)))

    (note that it is ‘==’ and not ‘=’). Sometimes bquote() gives a more compact form, e.g., title(bquote(hat(a) = .(ahat)))

    where subexpressions enclosed in ‘.()’ are replaced by their values.

    demo(plotmath) is also useful.


    In this case, you can use either

    title(substitute(R^2 = rsq, list(rsq = format(rsquarelm2, digits = 2))))
    

    or

    title(bquote(R^2 == .(format(rsquarelm2, digits = 2))))
    

    (format is more appropriate here than round, since you want to control how the value is displayed rather than creating an approximation of the value itself.)

提交回复
热议问题