将指数趋势线添加到ggplot
Add exponential trend line to ggplot
我有一个名为 daph 的数据集
daph <- read.table(text = "t v
20 19
25 78.2
30 254.8
",header = TRUE, sep = "")
我想要做的是将指数趋势线添加到具有这些值的条形图中
ggplot(data=daph, aes(x=t, y=v, width=1)) +
geom_bar(stat="identity", fill="steelblue") +
theme_minimal(base_size=18) +
geom_smooth(method = 'nls', formula = y ~ a * exp(b * x), se = FALSE, method.args = list(start = list(a = 1, b = 1)))
但它每次都给我一个错误信息(奇异梯度,粗略翻译)。
我想这与我的初始值有关,但我对数学的了解还不够,无法了解很多。
也许你们中的一些人可以帮助我 :)
众所周知,nls 很难适应。对于您的场景,请尝试提供更好的起始值或考虑改用线性模型,您可以在 post:
中查看
lmfit = lm(log(v) ~ t,data=daph)
A = coef(lmfit)[1]
B = coef(lmfit)[2]
ggplot(data=daph, aes(x=t, y=v, width=1)) +
geom_bar(stat="identity", fill="steelblue") +
theme_minimal(base_size=18) +
geom_smooth(method = 'nls', formula = y ~ a * exp(b * x),
se = FALSE, method.args = list(start = list(a = A, b = B)))
数据:
structure(list(t = c(9, 21, 29), v = c(19, 78.2, 254.8)), row.names = c(NA,
-3L), class = "data.frame")
我有一个名为 daph 的数据集
daph <- read.table(text = "t v
20 19
25 78.2
30 254.8
",header = TRUE, sep = "")
我想要做的是将指数趋势线添加到具有这些值的条形图中
ggplot(data=daph, aes(x=t, y=v, width=1)) +
geom_bar(stat="identity", fill="steelblue") +
theme_minimal(base_size=18) +
geom_smooth(method = 'nls', formula = y ~ a * exp(b * x), se = FALSE, method.args = list(start = list(a = 1, b = 1)))
但它每次都给我一个错误信息(奇异梯度,粗略翻译)。
我想这与我的初始值有关,但我对数学的了解还不够,无法了解很多。
也许你们中的一些人可以帮助我 :)
众所周知,nls 很难适应。对于您的场景,请尝试提供更好的起始值或考虑改用线性模型,您可以在 post:
中查看lmfit = lm(log(v) ~ t,data=daph)
A = coef(lmfit)[1]
B = coef(lmfit)[2]
ggplot(data=daph, aes(x=t, y=v, width=1)) +
geom_bar(stat="identity", fill="steelblue") +
theme_minimal(base_size=18) +
geom_smooth(method = 'nls', formula = y ~ a * exp(b * x),
se = FALSE, method.args = list(start = list(a = A, b = B)))
数据:
structure(list(t = c(9, 21, 29), v = c(19, 78.2, 254.8)), row.names = c(NA,
-3L), class = "data.frame")