本文介绍了如何覆盖ggplot2 scatterplot上的lm对象的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一些数据, calvarbyruno.1 1L,1L,1L,1L,1L,1L,1L),.Label = c(1,2 , 3),类= 因子), 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( 目标, 运行, PAR, PredLin, PredQuad),行。 names = c(NA,8L),class =data.frame) calweight 为此'创建了一个线性和二次lm模型 callin.1 calquad.1 $ b 然后我可以使用ggplot2绘制数据值 qplot(PAR,Nominal,data = calvarbyruno.1) 但无法工作如何覆盖代表两个lm对象的行......任何想法?解决方案最简单的选择是使用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),color =red) + geom_point()+ coord_flip() 或者您可以使用预测值创建一个新的数据集。 newdata< - data.frame(Nominal = pretty(calvarbyruno.1 $ Nominal,100)) newdata $ Linear newdata $ Quadratic require(reshape2) newdata< - melt(newdata,id.vars =Nominal, (x = PAR,y =标称,权重=标称^ calweight))+ geom_line(data = newdata,aes(x = value,color = Model))+ geom_point() 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 <- -2for which I've created both a linear and a quadratic lm modelcallin.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 ggplot2qplot(PAR,Nominal,data=calvarbyruno.1)But can't work out how to overlay a line representing the two lm objects... Any ideas ? 解决方案 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()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() 这篇关于如何覆盖ggplot2 scatterplot上的lm对象的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-31 15:02