我试图用 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, 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)
输出:或这个:
# 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)
输出:关于r - 无法在 R 中使用 ggplot2 调整对数曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65672847/