2016-09-30 25 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