nls适合,但在geom_smooth内部失败

问题描述 投票:1回答:2
我想使用ggplot使nls函数适合某些数据。在geom_smooth()外部使用时,nls函数运行良好,但在内部无法使用。

代码:

library(ggplot2) df <- structure(list(concentration = c(0, 0.5, 1.5, 4, 12, 35, 100), response = c(0.015, 0.03673, 0.07212, 0.1027, 0.1286, 0.1858, 0.1812)), class = "data.frame", row.names = c(NA, -7L)) df.fit <- nls(response ~ k0 + (ki*concentration/(KI + concentration)), df, start = list(k0 = 0.001, ki = 0.18, KI = 1)) coef(df.fit) plot <- ggplot(df, aes(concentration, response))+ geom_point()+ geom_smooth(method = "nls", se = F, method.args = list(formula = response ~ k0 + (ki*concentration/(KI + concentration)), start = list(k0 = 0.001, ki = 0.18, KI = 1))) plot

我想念什么?
r ggplot2 nls
2个回答
1
投票
您无法使用字符向量指定nls适合geom_smooth。从帮助文件:

method: Smoothing method (function) to use, accepts either a character vector, e.g. ‘"auto"’, ‘"lm"’, ‘"glm"’, ‘"gam"’, ‘"loess"’ or a function, e.g. ‘MASS::rlm’ or ‘mgcv::gam’, ‘stats::lm’, or ‘stats::loess’.

您可以指定一个新的预测向量,并使用geom_line进行绘制:

library(ggplot2) df <- structure(list(concentration = c(0, 0.5, 1.5, 4, 12, 35, 100), response = c(0.015, 0.03673, 0.07212, 0.1027, 0.1286, 0.1858, 0.1812)), class = "data.frame", row.names = c(NA, -7L)) df.fit <- nls(response ~ k0 + (ki*concentration/(KI + concentration)), df, start = list(k0 = 0.001, ki = 0.18, KI = 1)) coef(df.fit) df$pred <- predict(df.fit) plot <- ggplot(df, aes(concentration, response))+ geom_point()+ geom_line(aes(x = concentration, y = pred)) plot


0
投票
[抱歉,我现在问这个问题感到很尴尬。我只需要将geom_smooth公式中的变量“ response”和“ concentration”更改为“ y”和“ x”,如下所示。

library(ggplot2) df <- structure(list(concentration = c(0, 0.5, 1.5, 4, 12, 35, 100), response = c(0.015, 0.03673, 0.07212, 0.1027, 0.1286, 0.1858, 0.1812)), class = "data.frame", row.names = c(NA, -7L)) df.fit <- nls(response ~ k0 + (ki*concentration/(KI + concentration)), df, start = list(k0 = 0.001, ki = 0.18, KI = 1)) coef(df.fit) plot <- ggplot(df, aes(concentration, response))+ geom_point()+ geom_smooth(method = "nls", se = F, method.args = list(formula = y ~ k0 + (ki*x/(KI + x)), start = list(k0 = 0.001, ki = 0.18, KI = 1))) plot

© www.soinside.com 2019 - 2024. All rights reserved.