【问题标题】:plotting two simple linear regression scatterplots and lines on one graph from two data sets in R从 R 中的两个数据集在一张图上绘制两个简单的线性回归散点图和线条
【发布时间】:2017-08-31 03:04:59
【问题描述】:

我试过了:

plot(CORTMaglog~CORTlogB, data = data0, xlab="logCORTB", ylab="log CORT30- CORTB")
abline(lm(CORTMaglog ~ CORTlogB))

plot(CORTMaglog~CORTlogB, data = data1, xlab="logCORTB", ylab="log CORT30- CORTB")
abline(lm(CORTMaglog ~ CORTlogB))

现在有两个图表。

如何将来自两个不同数据集的两个图显示在一个带有折线图和散点图的图表上?

谢谢!

【问题讨论】:

    标签: r plot dataset regression


    【解决方案1】:

    您应该使用points。这是一个可重现的示例:

    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')
    

    编辑 回答 cmets 中的问题。要以您想要的方式使用plot 函数,您需要从predict 函数中提取预测。例如:

    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)
    

    【讨论】:

    • 感谢您的帮助!这是发生了什么: > data1 data0 x = 1:100 > y1 = lm(CORTMaglog~CORTlogB, data = data1) > y0 = lm(CORTMaglog~CORTlogB, data = data0) > plot(x,y1) xy.coords(x, y, xlabel, ylabel, log) 中的错误:“x”和“y”长度不同
    • 这是因为 y1 在你的情况下是一个 lm 对象。如果你想使用 plot(x,y1) 那么你需要做 y1 = predict(lm(CORTMaglog~CORTlogB, data = data1), newdata = data.frame(x))
    • 如果有帮助,欢迎采纳。
    • 这是有道理的。但是,我现在收到此错误消息:警告消息:'newdata' has 100 rows but variables found have 52 rows 感谢您帮助我!
    • 不要绘制 x,绘制你的 x 变量,它可能是 CORTlogB。您应该检查它的长度是否为 52,与您的 y 变量相同
    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 2021-04-28
    • 2019-01-14
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    相关资源
    最近更新 更多