如何在 ggplot2 散点图上覆盖 lm 对象的线条

发布于 2024-08-06 08:14:23 字数 1245 浏览 3 评论 0原文

我有一些数据,

calvarbyruno.1<-structure(list(Nominal = c(1, 3, 6, 10, 30, 50, 150, 250), Run = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"), 
    PAR = c(1.25000000000000e-05, 0.000960333333333333, 0.00205833333333334, 
    0.00423333333333333, 0.0322333333333334, 0.614433333333334, 
    1.24333333333333, 1.86333333333333), PredLin = c(-0.0119152187070942, 
    0.00375925114245899, 0.0272709559167888, 0.0586198956158952, 
    0.215364594111427, 0.372109292606959, 1.15583278508462, 1.93955627756228
    ), PredQuad = c(-0.0615895732702735, -0.0501563307416599, 
    -0.0330831368244257, -0.0104619953693943, 0.100190275883806, 
    0.20675348710041, 0.6782336426345, 1.04748729725370)), .Names = c("Nominal", 
"Run", "PAR", "PredLin", "PredQuad"), row.names = c(NA, 8L), class = "data.frame")
calweight <- -2

为此我创建了线性和二次 lm 模型,

callin.1<-lm(PAR~Nominal,data=calvarbyruno.1,weight=Nominal^calweight)
calquad.1<-lm(PAR~Nominal+I(Nominal^2),data=calvarbyruno.1,weight=Nominal^calweight)

然后我可以使用 ggplot2 绘制我的数据值

qplot(PAR,Nominal,data=calvarbyruno.1)

,但无法弄清楚如何覆盖代表两个 lm 对象的线...有什么想法吗?

I have some data,

calvarbyruno.1<-structure(list(Nominal = c(1, 3, 6, 10, 30, 50, 150, 250), Run = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"), 
    PAR = c(1.25000000000000e-05, 0.000960333333333333, 0.00205833333333334, 
    0.00423333333333333, 0.0322333333333334, 0.614433333333334, 
    1.24333333333333, 1.86333333333333), PredLin = c(-0.0119152187070942, 
    0.00375925114245899, 0.0272709559167888, 0.0586198956158952, 
    0.215364594111427, 0.372109292606959, 1.15583278508462, 1.93955627756228
    ), PredQuad = c(-0.0615895732702735, -0.0501563307416599, 
    -0.0330831368244257, -0.0104619953693943, 0.100190275883806, 
    0.20675348710041, 0.6782336426345, 1.04748729725370)), .Names = c("Nominal", 
"Run", "PAR", "PredLin", "PredQuad"), row.names = c(NA, 8L), class = "data.frame")
calweight <- -2

for which I've created both a linear and a quadratic lm model

callin.1<-lm(PAR~Nominal,data=calvarbyruno.1,weight=Nominal^calweight)
calquad.1<-lm(PAR~Nominal+I(Nominal^2),data=calvarbyruno.1,weight=Nominal^calweight)

I can then plot my data values using ggplot2

qplot(PAR,Nominal,data=calvarbyruno.1)

But can't work out how to overlay a line representing the two lm objects... Any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-08-13 08:14:23

最简单的选择是使用 geom_smooth() 并让 ggplot2 适合您的模型。

ggplot(calvarbyruno.1, aes(y = PAR, x = Nominal, weight=Nominal^calweight)) + 
    geom_smooth(method = "lm") + 
    geom_smooth(method = "lm", formula = y ~ poly(x, 2), colour = "red") + 
    geom_point() + 
    coord_flip()

Illustration using geom_smooth

或者您可以使用预测值创建一个新数据集。

newdata <- data.frame(Nominal = pretty(calvarbyruno.1$Nominal, 100))
newdata$Linear <- predict(callin.1, newdata = newdata)
newdata$Quadratic <- predict(calquad.1, newdata = newdata)
require(reshape2)
newdata <- melt(newdata, id.vars = "Nominal", variable.name = "Model")
ggplot(calvarbyruno.1, aes(x = PAR, y = Nominal, weight=Nominal^calweight)) + 
    geom_line(data = newdata, aes(x = value, colour = Model)) + 
    geom_point()

The easiest option is to use geom_smooth() and let ggplot2 fit the model for you.

ggplot(calvarbyruno.1, aes(y = PAR, x = Nominal, weight=Nominal^calweight)) + 
    geom_smooth(method = "lm") + 
    geom_smooth(method = "lm", formula = y ~ poly(x, 2), colour = "red") + 
    geom_point() + 
    coord_flip()

Illustration using geom_smooth

Or you can create a new dataset with the predicted values.

newdata <- data.frame(Nominal = pretty(calvarbyruno.1$Nominal, 100))
newdata$Linear <- predict(callin.1, newdata = newdata)
newdata$Quadratic <- predict(calquad.1, newdata = newdata)
require(reshape2)
newdata <- melt(newdata, id.vars = "Nominal", variable.name = "Model")
ggplot(calvarbyruno.1, aes(x = PAR, y = Nominal, weight=Nominal^calweight)) + 
    geom_line(data = newdata, aes(x = value, colour = Model)) + 
    geom_point()
南巷近海 2024-08-13 08:14:23

早些时候我问了一个相关的问题,哈德利有 这个答案很好。使用该文章中的预测函数,您可以向数据添加两列。每个模型一个:

calvarbyruno.1$calQuad <- predict(calquad.1)
calvarbyruno.1$callin <- predict(callin.1)

然后就是绘制点并将每个模型添加为一条线的问题:

ggplot() + 
geom_point(data=calvarbyruno.1, aes(PAR, Nominal), colour="green") + 
geom_line(data=calvarbyruno.1, aes(calQuad, Nominal), colour="red" ) + 
geom_line(data=calvarbyruno.1, aes(callin, Nominal), colour="blue" ) + 
opts(aspect.ratio = 1)

会产生这张漂亮的图片(是的,颜色需要一些工作):

这 sstatic.net/KLlby.png" rel="nofollow noreferrer">alt text
(来源:cerebralmastication.com

Earlier I asked a related question and Hadley had this good answer. Using the predict function from that post you can add two columns to your data. One for each model:

calvarbyruno.1$calQuad <- predict(calquad.1)
calvarbyruno.1$callin <- predict(callin.1)

Then it's a matter of plotting the point and adding each model in as a line:

ggplot() + 
geom_point(data=calvarbyruno.1, aes(PAR, Nominal), colour="green") + 
geom_line(data=calvarbyruno.1, aes(calQuad, Nominal), colour="red" ) + 
geom_line(data=calvarbyruno.1, aes(callin, Nominal), colour="blue" ) + 
opts(aspect.ratio = 1)

And that results in this nice picture (yeah the colors could use some work):

alt text
(source: cerebralmastication.com)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文