简体   繁体   English

在R中的两个数据集中的一张图上绘制两个简单的线性回归散点图和直线

[英]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.

相关问题 散点图以比较R中的两个数据集 - Scatterplots to compare two data sets in R 如何在R Studio中将两个数据集合并为一个并用两个数据集绘制一个图形 - How to combine two data sets into one and plotting one graph with both data sets in R Studio 将线性回归 model(> 3 个预测变量)中的两条(!)回归线绘制到具有置信区间(sjPlot 或 ggplot)的相同 plot 中 - Plotting two(!) regression lines from a linear regression model ( > 3 predictors) into the same plot with confidence intervals (sjPlot or ggplot) 在R中如何在不同数据帧中长度不相等的两个变量之间运行相关或简单线性回归 - In R how to run Correlation or simple linear Regression between two variables of unequal lengths from different data frames 将两条线性回归线放在一张图中 - Put two linear regression lines into one plot 在R中再次绘制两组数据 - Plotting two sets of data againnst one another in r 在 R 中用两条线绘制折线图 - plotting line graph with two lines in R R:使用ggplot在单个图上绘制两个散点图 - R: two scatterplots on single graph using ggplot R编程-具有等式约束的线性回归(两组虚拟变量) - R programming - Linear regression (two sets of dummy variables) with equality constraints 一张图中两个散点图中的因子水平排序 - Ordering of factor level in two scatterplots in one graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM