LoginSignup
0
1

More than 3 years have passed since last update.

Rの散布図中に決定係数と回帰式を書き込む

Posted at

ggplot2なら簡単だが、baseのplotだと思っていたより難しかった。

r2.R
xx1 <- rnorm(100)
yy1 <- -1*xx1+rnorm(100)
result <- lm(yy1~xx1)
slp <- round(result$coef[2], digits=2)
intc <- round(result$coef[1], digits=2)
plot(xx1,yy1,xlab="x",ylab="y",
     cex.axis = 1.2, cex.lab = 1.2, bty = "o", pch=16, col = "black", lwd=1.5)
text(x=-2, y=-2, as.expression(substitute(italic(R)^2 == r, list(r = round(summary(result)$r.sq, digits=2)))), cex=2)
if (intc > 0) {text(x=-2, y=-2.5, as.expression(paste0("y= ", slp, "x + ", intc)), cex=2) # y切片の符号で場合分け
  } else {text(x=-2, y=-2.5, as.expression(paste0("y= ", slp, "x ", intc)), cex=2)}

結果はこんな感じ。

image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1