如何在R中创建符号文本字符串 [英] How to create symbol text strings for plots in R
问题描述
我有一个情节,并希望在绘图区域中添加一些回归统计(如F,R2,p)。我熟悉 text()
,但是一直没有找到一个综合的信息来源,例如关于如何使用数学符号,子标题和上标等构建文本字符串的例子任何有详细示例的来源都非常感激。
I have a plot and would like to add some regression statistics (e.g. F, R2, p) in the plot area. I am familiar with text()
, but have been unable to find a comprehensive source of information with examples on how to build text strings with mathematical symbols, sub-and superscripts, etc. Any sources with detailed examples greatly appreciated.
例如,我有一个简单的线性回归,我想从中提取统计信息并将其添加到我的情节。例如
For example, I have a simple linear regression that I would like to extract the stats from and add them to my plot. For example
reg1 <- lm(WW1 ~ PC1, data = WW_Data)
我想要像 F1,69 = 14.38,p = 0.001,R2adj = 0.16
其中1,69和adj为下标,p为斜体。
I would like to have something like F1,69 = 14.38, p = < 0.001, R2adj = 0.16
where "1,69" and "adj" is subscript, and "p" is in italics.
编辑:
感谢 @Backlin
对我的问题我已经扩展了一点,所以如果你得到一个非常重要的p值,代码将代替< 0.001,并将所有的统计数据舍入到2位小数,除了我被舍入为3的p值。 p>
Thanks to @Backlin
for a great answer to my question. I have expanded on it a bit so that if you get a highly significant p-value the code substitutes "< 0.001" and rounded all the statistics to 2 decimal places, except the p-value which I have rounded to 3.
WW_Data <- data.frame(WW1=rnorm(10), PC1=1:10)
reg1 <- lm(WW1~PC1, WW_Data)
sreg1 <- summary(reg1)
plot(0, 0)
text(0, .2, eval(substitute(
expression(list(F[list(fn,fd)]==fv,italic(p)==pv,R[adj]^2==R2adj)),
list(fv = round(sreg1$fstatistic[1],2), fn = sreg1$fstatistic[2],
fd = sreg1$fstatistic[3], pv = ifelse(sreg1$coefficients["PC1",4] < 0.001, "< 0.001",round(sreg1$coefficients["PC1",4],3)),
R2adj = round(sreg1$adj.r.squared,2)))))
推荐答案
我自己一直在努力,但实际上都在?plotmath
。您的表达式将是以下内容,
I have struggled a lot with it myself too, but it is actually all in ?plotmath
. Your expression would be the following,
# Fixed expression
text(x, y, expression(list(F[list(1,69)]==14.38,italic(p)<0.001,R[adj]^2==0.16)))
# Using the values of your lm
sreg1 <- summary(reg1)
text(x, y, eval(substitute(
expression(list(F[list(fn,fd)]==fv,italic(p)==pv,R[adj]^2==R2adj)),
list(fv = sreg1$fstatistic[1], fn = sreg1$fstatistic[2],
fd = sreg1$fstatistic[3], pv = sreg1$coefficients["PC1",4],
R2adj = sreg1$adj.r.squared))))
这是一个虚拟的例子。
WW_Data <- data.frame(WW1=rnorm(10), PC1=1:10)
reg1 <- lm(WW1~PC1, WW_Data)
sreg1 <- summary(reg1)
plot(0, 0)
text(0, .2, eval(...)) # The expression above
这篇关于如何在R中创建符号文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!