无法在R中使用ggplot2调整对数曲线
我正试图用无法在R中使用ggplot2调整对数曲线,r,ggplot2,curve,logarithm,R,Ggplot2,Curve,Logarithm,我正试图用ggplot2绘制一条可调对数曲线。通过各种堆栈溢出帖子,我能够创建以下内容: key <- c(1,2,300) value <- c(10, 20, 300) dummyData <- data.frame(key, value) # ggplot graph plotting graph <- ggplot(dummyData, mapping = aes(x=key, y=value))+ stat_smooth( method = 'nl
ggplot2
绘制一条可调对数曲线。通过各种堆栈溢出帖子,我能够创建以下内容:
key <- c(1,2,300)
value <- c(10, 20, 300)
dummyData <- data.frame(key, value)
# ggplot graph plotting
graph <- ggplot(dummyData, mapping = aes(x=key, y=value))+
stat_smooth(
method = 'nls',
formula = 'y ~ a * log(x) + b',
method.args = list(
start = list(a = 1, b = 1)
),
se = FALSE
)
print(graph)
键尝试从nls()
启用下/上选项。如您所见,曲线已发生变化:
library(ggplot2)
# ggplot graph plotting 1
graph <- ggplot(dummyData, mapping = aes(x=key, y=value))+
stat_smooth(
method = 'nls',
formula = 'y ~ a * log(x) + b',
method.args = list(
start = list(a = 1, b = 1),
lower = c(1, 1),
upper = c(1, 1),algorithm = "port"
),
se = FALSE
)
print(graph)
# ggplot graph plotting 2
graph <- ggplot(dummyData, mapping = aes(x=key, y=value))+
stat_smooth(
method = 'nls',
formula = 'y ~ a * log(x) + b',
method.args = list(
start = list(a = 999, b = 999),
lower = c(999, 999),
upper = c(999, 999),algorithm = "port"
),
se = FALSE
)
print(graph)
库(ggplot2)
#ggplot图形绘制1
图形化您的代码运行良好,问题是什么?@Duck问题是当我更改a
和/或b
值时对数曲线没有被调整(通过start=list(a=9999,b=9999)
),我认为start
参数只是初始化拟合,但它仍然会尝试收敛到最佳拟合(由数据决定,因此不会改变)。因此,如果您想强制直线,也许您可以添加其他数据点或使用其他参数约束拟合。@DanAdams我可以约束拟合的其他参数是什么?还有,我有没有办法告诉它不要收敛到最佳拟合?我不是这方面的专家,但您可以检查?nls
,这会导致?nls.control
告诉我们可以使用maxiter=1
限制迭代次数。但这就引出了一个问题,即为什么要强制对某些任意参数进行次优拟合。也许如果您对您的情况多解释一点,您可以得到另一个更合理的解决方案?
# ggplot graph plotting 3
graph <- ggplot(dummyData, mapping = aes(x=key, y=value))+
stat_smooth(
method = 'nls',
formula = 'y ~ a * log(x) + b',
method.args = list(
start = list(a = 1, b = 1),
lower = c(1, 1),
upper = c(10, 20),algorithm = "port"
),
se = FALSE
)
print(graph)