ggplot2 scale_x_log10()破坏/不适用于通过stat_function()绘制的函数 [英] ggplot2 scale_x_log10() destroys/doesn't apply for function plotted via stat_function()
问题描述
我终于设法在ggplot2中将我的自定义拟合函数绘制在我的数据上,但是当我记录转换x轴时,绘制的函数完全搞砸了。
看起来像 scale_x_log10()
只适用于绘制数据,但不适用于函数。
如何使函数以正确的比例出现?
以下是Hadley的stat_function()文档的修改示例:
x < - rnorm(100)
qplot(x,geom =density)+ stat_function(fun = dnorm,color =red)
现在使用log10 x轴:
qplot(x,geom =density)+ stat_function(fun = dnorm,color =red)+ scale_x_log10()
更新
好的,我想我的例子并不是很有帮助,所以我尝试了不同的方式:基本上我想要的是再现一个我用曲线()做的曲线。
#函数
HillFunction< - 函数(ec50,hill,rmax,x){rmax /(1+(ec50 / x)^ hill)}
#拟合参数
hill.args< - list(ec50 = 10 ^ -2 ,Hill = .7,rmax = 1)
曲线(HillFunction(ec50 = hill.args $ ec50,rmax = hill.args $ rmax,hill = hill.args $ hill,x) = 10 ^ -5,to = 10 ^ 5,log =x)
)如预期的那样给我一个平滑的S形曲线。现在我尝试用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 $现在如果我绘制
p
一切看起来都不错,就像curve()$ c
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $,$ = hill.args $ rmax,hill = hill.args $ hill,x),从= 10 ^ -5到= 10 ^ 5)
如果我转换坐标系,我会得到一个S形曲线,但不会平滑,曲线看起来很陡,但也许是来自x缩放:
p + coord_trans(x =log10)
如果我将x尺度定义为对数尺度,但停在10 ^ 0:
p + scale_x_log10()
我得到以下警告:删除了1500行值(geom_path)。
建议的解决方案
以下代码是让ggplot2执行我认为您正在尝试完成的一种方法。
library( ggplot2)
#定义函数。装配参数包含在默认值中。
HillFunction =函数(x,ec50 = 0.01,hill = 0.7,rmax = 1.0){
result = rmax /(1 +(ec50 / x)^ hill)
return(result)
}
#创建x使得点在日志空间中均匀分布。
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),color =red)+
scale_x_log10()+
opts(title =图1.建议)
png(plot_1.png,height = 450,width = 450)
print(plot_1)
dev.off()
stat_function()的问题
-
stat_function()
试图为
负值x
评估
HillFunction()
code>。这就是为什么你得到缺失值
的错误。 对于任何
x
的0到1之间的值,可以使用c> HillFunction()。它选择
x 在线性空间中,
忽略 scale_x_log10()
已被指定。
下面的代码说明了这个问题,但我仍然无法解释为什么 stat_function()
从<$ c发散很多
plot_2 = ggplot(dat,aes(x = x) ,y = y_fit))+
geom_point()+
stat_function(fun = HillFunction,color =red)+
scale_x_log10()+
opts(title =Figure 2.)stat_function()行为不当?)
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 )
曲线(HillFunction,col =red,add = TRUE)
title(Figure 3. curve()behaviour as expected)
dev.off()
I finally managed to plot my custom fitted function over my data in ggplot2 but when I log-transform the x axis the plotted function gets totally messed up.
It looks like the scale_x_log10()
applies only to the plotted data but not to the function.
How can I make the function to appear in the correct scale?
Here is an modified example from Hadley's stat_function() documentation:
x <- rnorm(100)
qplot(x, geom="density") + stat_function(fun = dnorm, colour="red")
and now with log10 x-axis:
qplot(x, geom="density") + stat_function(fun = dnorm, colour="red") + scale_x_log10()
update
Okay, I think my example was not very helpful so I try it differently:
essentially what I want is to reproduce a plot I did with curve(). I fitted a Hill function to my data and now want to plot it:
# 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")
so curve() gives me a smooth sigmoidal curve as expected. Now I try to reproduce the same plot with ggplot:
I add some data from 10^-5 to 10^5 just to define the plotting range, not sure if there are better ways
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")
now if I plot p
everything looks fine, like the curve()
plot without the logscale:
p
curve(HillFunction(ec50=hill.args$ec50,rmax=hill.args$rmax, hill=hill.args$hill,x),from=10^-5, to=10^5)
If I transform the coordinate system I get a sigmoidal curve but not smooth at all and the curve looks way to steep, but maybe that comes from x-scaling:
p + coord_trans(x="log10")
And if I define the x scale to be a log-scale the plot looks smooth but stops at 10^0:
p + scale_x_log10()
and I get the following warning: Removed 1500 rows containing missing values (geom_path).
Proposed Solution
The following code is one way to get ggplot2 to do what I think you are trying to accomplish.
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()
Problems With stat_function()
stat_function()
is trying to evaluateHillFunction()
for negative values ofx
. This why you get themissing values
error.stat_function() is not evaluating
HillFunction()
for anyx
values between 0 and 1. It is selectingx
in linear space, ignoring thatscale_x_log10()
has been specified.
The following code illustrates the problem, but I still can't explain why stat_function()
diverges so much from y_fit
in Figure 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()
这篇关于ggplot2 scale_x_log10()破坏/不适用于通过stat_function()绘制的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!