简体   繁体   English

在 R 中使用 bquote 上标后的下标

[英]subscript after superscript using bquote in R

I am trying to plot a specific R-square metric, the R2 relative to the 1:1 line.我正在尝试绘制一个特定的 R 平方指标,即相对于 1:1 线的 R2。 Here are some code to generate data and a plot.下面是一些生成数据和绘图的代码。 (I realize I am only calculating standard r-squared here, but thats fine for the purposes of working out the code). (我意识到我只是在这里计算标准 r 平方,但这对于计算代码来说很好)。

#generate data.
set.seed(1234)
x <- rnorm(100)
y <- x*0.7 + rnorm(100)
mod <- lm(y~x)

#develop rsq label.
rsq <- round(summary(mod)$r.squared, 2)
rsq.1.lab <- bquote(R^2 [1:1] == .(rsq))

#drop plot and rsq label.
plot(y ~ x)
mtext(rsq.1.lab, side = 3, line = -2, adj = 0.05)

The plot looks like this:情节是这样的: 在此处输入图片说明

This is pretty close, but the subscript is actually relative to the superscript, rather than being relative to the letter R. How can I change this?这非常接近,但下标实际上是相对于上标的,而不是相对于字母 R。我该如何更改? Looking for solutions that use base R, ideally keeping bquote() .寻找使用基础 R 的解决方案,最好保留bquote()

Use { / } grouping:使用{ / }分组:

rsq.1.lab <- bquote({R^2} [1:1] == .(rsq))

选项1

or或者

rsq.1.lab <- bquote({R [1:1]}^2 == .(rsq))

选项 2

or even a somewhat ridiculous甚至有点可笑

rsq.1.lab <- bquote(R * atop(2, "1:1") == .(rsq))

选项 3

though we can reduce the font size a little using虽然我们可以使用

rsq.1.lab <- bquote(R * scriptstyle(atop(2, "1:1")) == .(rsq))

选项 4

Much of this is suggested/documented in ?plotmath . ?plotmath建议/记录了其中的大部分?plotmath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM