r - 无法在 R 中使用 ggplot2 调整对数曲线

标签 r ggplot2 curve logarithm

我试图用 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)
输出:
enter image description here
enter image description here
或这个:
# 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)
输出:
enter image description here

关于r - 无法在 R 中使用 ggplot2 调整对数曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65672847/

相关文章:

R:写入 CSV 并通过管道继续

r - 在 R 中的轴上显示日期

r - 不带coord_flip的水平ggplot2::geom_violin

Swift - 添加自定义曲线扩展后,它不会渲染以编程方式创建的 View ,仅渲染使用 Interface Builder 创建的 View

javascript - Javascript 中带有加减号 (±) 的极坐标曲线方程

java - 通用路径曲线到

r - 在ggplot2中标记特定点

r - 如何在 Logit 回归的调查数据上使用 GAM (mgcv) 中的样本权重?

r - ggplot geom_linerange 中的 Alpha 由 Mac 上的观察次数决定

r - 制作箱线图时如何在函数内部的ggplot2中对组使用aes_string