R图中不出现二次拟合线

时间:2015-03-06 14:51:13

标签: r ggplot2 surface quadratic

当我在R中运行以下脚本时,拟合线不会出现在图中。我希望它出现。你能救我吗?

rm(list=ls())
# Surface tension
surft <- c(49.1, 51.7, 50.7, 52.0, 53.5, 52.9, 55.1, 59, 62.5, 66.4, 69.0, 73.8) # surface tension of mixtures

# Mole fractions
M1 <- c(62.0678) # Molar mass ethylene glycol
M2 <- c(18.01528) # Molar mass water
m1 <- c(20, 19.3474, 19.3727, 14.4605, 16.9556, 17.9408, 16.0283, 11.6572, 7.5394, 5.0984, 2.0971, 0) # mass of ethylene glycol in mixture
m2 <- c(0, 0.4192, 1.0761, 1.3379, 2.3457, 3.5900, 6.3889, 12.0517, 11.8217, 18.4019, 17.0114, 20) # mass of water in mixture
mf <- c((m1*M2)/(m1*M2+m2*M1)) # mole fractions ethylene glycol
mfwater <- c((m2*M1)/(m2*M1+m1*M2))

# Plot surface tension / mole fraction
plot(mf, surft, type="p", xlab="Mole fraction ethylene glycol", ylab="Surface tension / mN/m")

#Fitting with Connors-Wright Model
#a <- c(1)
#b <- c(1)
#surftfit <- c((1+(a*mfwater)/(1-b*mfwater))*mf)
#lines(mf, surftfit)

fit <- lm(mf ~ surft + I(surft^2))
fitted <- 0.002355*mf^2-0.329045*mf+11.492344
lines(mf, fitted)

ggplot2太棒了,非常感谢你的帮助。我现在有以下代码:

rm(list=ls())
# Surface tension
surft <- c(49.1, 51.7, 50.7, 52.0, 53.5, 52.9, 55.1, 59, 62.5, 66.4, 69.0, 73.8) # surface tension of mixtures

dev <- c(2.04229, 0.55532, 0.76122, 0.52876, 0.72262, 1.67266, 1.49312, 0.55022, 
0.42132, 0.97973, 0.32890, 0.38697)  #standard deviation


# Mole fractions
M1 <- c(62.0678) # Molar mass ethylene glycol
M2 <- c(18.01528) # Molar mass water
m1 <- c(20, 19.3474, 19.3727, 14.4605, 16.9556, 17.9408, 16.0283, 11.6572, 7.5394, 5.0984, 2.0971, 0) # mass of ethylene glycol in mixture
m2 <- c(0, 0.4192, 1.0761, 1.3379, 2.3457, 3.5900, 6.3889, 12.0517, 11.8217, 18.4019, 17.0114, 20) # mass of water in mixture
mf <- c((m1*M2)/(m1*M2+m2*M1)) # mole fractions ethylene glycol
mfwater <- c((m2*M1)/(m2*M1+m1*M2))


# Plot surface tension / mole fraction
#plot(mf, surft, type="p", xlab="Mole fraction ethylene glycol", ylab="Surface tension / mN/m")



fit <- lm(mf ~ surft + I(surft^2))
#fitted <- 0.002355*mf^2-0.329045*mf+11.492344
#lines(mf, fitted)

#Fitting with Connors-Wright Model
#a <- c(0.9)
#b <- c(0.5)
#surftfit <- c((1+(a*mfwater)/(1-b*mfwater))*mf)
#lines(mf~surft, col="red")

#plot(mf, surft, type="p", xlab="Mole fraction ethylene glycol", ylab="Surface tension / mN/m")
#ft = fitted(fit)
#lines(mf, ft)
dd <-data.frame(mf,surft)
ggplot(dd, aes(x=mf, y = surft))+geom_point()+stat_smooth(method="lm", formula = y~x+I(x^2))

如何以误差条的形式显示图中的标准偏差? 还有一个问题:对于这一点,二次拟合似乎并不是极其简单。我猜想对于0 <= x <= 1,y = 1 / x(双曲线)形式的拟合将是更好的拟合。我怎么能适应这种双曲线?

1 个答案:

答案 0 :(得分:0)

您的代码中有许多不起作用的内容,因此您不清楚自己想做什么或者想要适合什么。你应该试试这个代码

plot(surft, mf, type="p", xlab="Mole fraction ethylene glycol", ylab="Surface tension / mN/m")
ft = fitted(fit)
lines(surft, ft)