gpt4 book ai didi

r - ggplot2 scale_x_log10() 破坏/不适用于通过 stat_function() 绘制的函数

转载 作者:行者123 更新时间:2023-12-03 11:49:42 25 4
gpt4 key购买 nike

我终于设法在 ggplot2 中的数据上绘制了我的自定义拟合函数,但是当我对 x 轴进行对数变换时,绘制的函数变得完全困惑。
它看起来像 scale_x_log10()仅适用于绘制的数据,而不适用于函数。

如何使函数以正确的比例显示?

这是 Hadley 的 stat_function() 文档中的修改示例:

x <- rnorm(100)
qplot(x, geom="density") + stat_function(fun = dnorm, colour="red")

现在使用 log10 x 轴:
qplot(x, geom="density") + stat_function(fun = dnorm, colour="red") + scale_x_log10()

更新

好的,我认为我的示例不是很有帮助,所以我尝试了不同的方法:

基本上我想要的是重现我用曲线()做的图。我为我的数据安装了 Hill 函数,现在想要绘制它:
# the function
HillFunction <- function(ec50,hill,rmax,x) {rmax/(1+(ec50/x)^hill)}
# fitted parameters
hill.args <- list(ec50=10^-2, hill=.7, rmax=1)

curve(HillFunction(ec50=hill.args$ec50,rmax=hill.args$rmax, hill=hill.args$hill,x),from=10^-5, to=10^5,log="x")

所以 curve() 给了我预期的平滑 sigmoidal 曲线。现在我尝试用 ggplot 重现相同的图:

我添加了一些从 10^-5 到 10^5 的数据只是为了定义绘图范围,不确定是否有更好的方法
p <- ggplot(data=data.frame(x=c(10^-5:10^5)), aes(x=x))  + stat_function(fun=HillFunction, args=hill.args, n=3000, color="red")        

现在,如果我绘制 p一切看起来都很好,就像 curve()没有对数刻度的绘图:
p
curve(HillFunction(ec50=hill.args$ec50,rmax=hill.args$rmax, hill=hill.args$hill,x),from=10^-5, to=10^5)

如果我变换坐标系,我会得到一条 sigmoidal 曲线,但根本不平滑,曲线看起来很陡峭,但这可能来自 x 缩放:

p + coord_trans(x="log10")

如果我将 x 比例定义为对数比例,则该图看起来很平滑,但在 10^0 处停止:
p + scale_x_log10()

我收到以下警告: Removed 1500 rows containing missing values (geom_path).

最佳答案

建议的解决方案

以下代码是让 ggplot2 执行我认为您要完成的操作的一种方法。

library(ggplot2)

# Define function. Fitted parameters included as default values.
HillFunction = function(x, ec50=0.01, hill=0.7, rmax=1.0) {
result = rmax / (1 + (ec50 / x)^hill)
return(result)
}

# Create x such that points are evenly spread in log space.
x = 10^seq(-5, 5, 0.2)
y_fit = HillFunction(x)
y_raw = y_fit + rnorm(length(y_fit), sd=0.05)

dat = data.frame(x, y_fit, y_raw)

plot_1 = ggplot(data=dat, aes(x=x, y=y_raw)) +
geom_point() +
geom_line(data=dat, aes(x=x, y=y_fit), colour="red") +
scale_x_log10() +
opts(title="Figure 1. Proposed workaround.")

png("plot_1.png", height=450, width=450)
print(plot_1)
dev.off()

Figure 1

stat_function() 的问题
  • stat_function()正在尝试评估 HillFunction()为了x 的负值.这就是您获得 missing values 的原因错误。
  • stat_function() 未评估 HillFunction()对于任何 x 0 到 1 之间的值。它正在选择 x在线性空间中,
    忽略 scale_x_log10()已指定。

  • 下面的代码说明了这个问题,但我仍然无法解释为什么 stat_function()y_fit 相差甚远在图 2 中。
    plot_2 = ggplot(dat, aes(x=x, y=y_fit)) +
    geom_point() +
    stat_function(fun=HillFunction, colour="red") +
    scale_x_log10() +
    opts(title="Figure 2. stat_function() misbehaving?")

    png("plot_2.png", height=450, width=450)
    print(plot_2)
    dev.off()


    png("plot_3.png", height=450, width=450)

    plot(x, y_fit, pch=20, log="x")
    curve(HillFunction, col="red", add=TRUE)
    title("Figure 3. curve() behaving as expected.")

    dev.off()

    Figure 2

    enter image description here

    关于r - ggplot2 scale_x_log10() 破坏/不适用于通过 stat_function() 绘制的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9382032/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com