二次拟合线未出现在 R 图中

Quadratic fitted line doesn't appear in R plot

当我 运行 在 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(双曲线)形式的拟合会更好。我怎样才能拟合这个双曲线?

您好,有很多东西在您的代码中不起作用,因此不清楚您想要做什么或您想要适应什么。你应该试试这个代码

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