2016-09-30 50 views
3

我需要包括在图中的文本字幕在R.我基本上要包括一个回归系数(R^2) - 这是我与下面的代码文本与R中数学

text(-8, 2, bquote(R^2 == .(round((summary(fit1)$r.squared),2)))) 

完成但是现在我想要做一些改变。我希望R^2使用斜体,并添加其他文本信息。

我可以使R^2个斜体这样

text(-8, 2, expression(italic(R^2))) 

但是,有没有办法做到这一点使用bquote(),所以我可以包括回归系数呢?

第二个问题是我该如何在同一标题中包含其他文字。

比如我自己也尝试这个代码绘制R^2之前的一些文本 - 但这里的数学完全不

text(-9, 2, paste("Linear Regression \n R^2" , round((summary(fit1)$r.squared),2))) 

任何帮助,将不胜感激工作。

谢谢。

回答

1

我们只是italic

text(-8, 2, bquote(italic(R^2) == .(round((summary(fit1)$r.squared),2)))) 

包装它,并添加一些字符串

text(-8, 2, bquote('Linear Regression'~italic(R^2) == .(round((summary(fit1)$r.squared),2)))) 

使用重复的例子

set.seed(425) 
x <- sample(10, 10, replace=TRUE) 
y <- sample(20, 10, replace=TRUE) 
fit1 <- lm(x~y) 
plot(x, y, xlim = c(0,10), ylim = c(0,20)) 
text(8, 2, bquote('Linear Regression'~italic(R^2) == .(round((summary(fit1)$r.squared),2)))) 

enter image description here

+0

太好了,谢谢!作为最后一个问题 - 是否有一种方法可以使用\ n并将R^2放在新行上? – lily23

+0

@ lily23也许你需要'text(8,3,'Linear Regression'); (round((summary(fit1)$ r.squared),2))))'(例如我显示的) – akrun

+1

Thanks - that'会做这个工作=) – lily23