2016-05-09 2 views
1

2つの線形回帰プロットを比較し、それぞれのeqnsをプロットまたはわずかに上に印刷します。私は方程式をr2の値で表示させ、それぞれのプロットに(理想的には1行に)配置する方法を知らない。テキストファイルは次のとおりです。2つのプロットにr2値を含む線方程式を書くには

num abs 
0.875 0.128 
1.75 0.262 
2.5 0.579 
5 1.292 
10 2.151 
.875 0.185 
1.75 0.273 
2.5 0.507 
5 1.561 
10 2.581 

ありがとうございました。

d <- read.table("C:/Users/trinh/OneDrive/Documents/R scripts/BSA.txt", header=T) 
attach(d) 
da <- d[1:5,] 
da2 <- d[6:10,] 
reg.da <- lm(abs[1:5]~ num[1:5]) 
reg.da2 <- lm(abs[6:10]~num[6:10]) 
par(mfrow=c(1,2)) 

#1st plot 
plot(da,main="Regression line for BSA-1",xlab="[BSA]",ylab="Abs") 
abline(reg.da) 

#second plot 
plot(da2,main="Regression line for BSA-2",xlab="[BSA]",ylab="Abs") 
abline(reg.da2) 

#establish b intercept/slope and r^2 
cf<-round(coef(reg.da2),2) 
r2.1<-format(summary(reg.da2)$r.squared, digits = 3) 

#first eqn: 
eq1<-paste("y = ",cf[1], 
    ifelse(sign(cf[2])==1, " + ", " - "), cf[2],"x") 
mtext(eq1,side=3) 
mtext(bquote(r^2 == .(r2.1)),adj=1) 

#establish b intercept/slope and r^2 
cf1<-round(coef(reg.da),2) 
r2.2<-format(summary(reg.da)$r.squared, digits = 3) 

#second eqn: 
eq2<-paste("y =", cf1[1], 
    ifelse(sign(cf1[2])==1, " + ", " - "), cf1[2],"x") 
mtext(eq2,side=3) 
mtext(bquote(r^2 == .(r2.2)),adj=1) 

答えて

0

2番目のプロットを追加する前に、最初のプロットのテキストを配置する必要があります。次のコードを使用して、データに基づいてサンプル図を生成しました。 mtext()の引数にcex=0.7を追加して、テキストのサイズを縮小し、方程式がr-二乗値を妨害しないようにしました。

par(mfrow=c(1,2)) 

#1st plot 
plot(da,main="Regression line for BSA-1",xlab="[BSA]",ylab="Abs") 
abline(reg.da) 
#establish b intercept/slope and r^2 
cf<-round(coef(reg.da2),2) 
r2.1<-format(summary(reg.da2)$r.squared, digits = 3) 

#first eqn: 
eq1<-paste("y = ",cf[1], 
      ifelse(sign(cf[2])==1, " + ", " - "), cf[2],"x") 
mtext(eq1,side=3,adj=0,cex=0.7) 
mtext(bquote(r^2 == .(r2.1)),adj=1,cex=0.7) 

#second plot 
plot(da2,main="Regression line for BSA-2",xlab="[BSA]",ylab="Abs") 
abline(reg.da2) 

#establish b intercept/slope and r^2 
cf1<-round(coef(reg.da),2) 
r2.2<-format(summary(reg.da)$r.squared, digits = 3) 

#second eqn: 
eq2<-paste("y =", cf1[1], 
      ifelse(sign(cf1[2])==1, " + ", " - "), cf1[2],"x") 
mtext(eq2,side=3,adj=0,cex=0.7) 
mtext(bquote(r^2 == .(r2.2)),adj=1,cex=0.7) 

enter image description here

+0

感謝!あなたの回答/解決に感謝します。 –

関連する問題