[英]plotting two simple linear regression scatterplots and lines on one graph from two data sets in R
I have tried: 我努力了:
plot(CORTMaglog~CORTlogB, data = data0, xlab="logCORTB", ylab="log CORT30- CORTB")
abline(lm(CORTMaglog ~ CORTlogB))
and 和
plot(CORTMaglog~CORTlogB, data = data1, xlab="logCORTB", ylab="log CORT30- CORTB")
abline(lm(CORTMaglog ~ CORTlogB))
and now have two graphs. 现在有两个图。
How do I have both plots from two different data sets on one graph with lines and scatterplots? 如何在一张图上用线和散点图绘制两个不同数据集的两个图?
Thank you! 谢谢!
You should use points
. 您应该使用points
。 Here is a reproducible example: 这是一个可重现的示例:
x = 1:100
y1 = x^1.2 + x*rnorm(100,0,1)
y2 = 2*x^1.2 + x*rnorm(100,1,0.5)
plot(x,y1)
plot(x,y2)
data1 = cbind(x,y1)
data2 = cbind(x,y2)
plot(y2 ~ x, data=data2,col='blue')
abline(lm(y2 ~ x),col='blue')
points(y1 ~ x, data=data1,col='red')
abline(lm(y1 ~ x),col='red')
Edit To answer the question in the comments. 编辑以在评论中回答问题。 To use the plot
function the way you want to, you need to extract the predictions from the predict
function. 要以所需方式使用plot
函数,需要从predict
函数中提取预测。 For example: 例如:
x = 1:100
y1 = x^1.2 + x*rnorm(100,0,1)
data1 = data.frame(cbind(x,y1))
fit = lm(y1~x, data=data1)
y = predict(fit, newdata = data.frame(x))
plot(x,y1)
lines(x,y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.