向 R 平方添加一个附加字母以在 ggplot 中进行注释 [英] Add an aditional letter to R squared for annotate in ggplot
问题描述
我想这是一个非常简单的问题,但我无法解决问题.我正在拟合一个线性混合效应模型,我想在我的图中插入边际和条件 R 平方.出于这个原因,我想给它们贴上标签:
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
通过该代码,我得到了 R²:
With that code I get the R²:
annotate(geom = 'text', label = paste("R^2 == ", 0.65), parse = TRUE)
然而,我尝试使用该代码,但我无法将字母放在 R² 后面.您有什么建议吗?
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?
推荐答案
一种方法是使用 bquote
代替.这将添加m"和c"作为R"的索引.我发现在情节中使用表达式总是有点繁琐,特别是如果你想变得非常花哨.
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)
由 reprex 包 (v2.0.0) 于 2021 年 6 月 7 日创建上>
Created on 2021-06-07 by the reprex package (v2.0.0)
这篇关于向 R 平方添加一个附加字母以在 ggplot 中进行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!