问题描述
我正在尝试使用 ggplot2
绘制可调整的对数曲线。从各种 Stack Overflow 帖子中,我能够创建这个:
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)
然而,不管我把start = list(a = 1,b = 1)
线放在什么极值上,我根本无法调整对数曲线。
解决方法
尝试启用 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,algorithm = "port"
),se = FALSE
)
print(graph)
# ggplot graph plotting 2
graph <- ggplot(dummyData,method.args = list(
start = list(a = 999,b = 999),lower = c(999,999),upper = c(999,se = FALSE
)
print(graph)
输出:
或者这个:
# ggplot graph plotting 3
graph <- ggplot(dummyData,upper = c(10,20),se = FALSE
)
print(graph)
输出: