[英]Plot function with x-axis in log-scale using ggplot
I want to plot a function (result of using drm()
from drc package) using ggplot2, with X-axis in log-scale. 我想使用ggplot2绘制一个函数(使用drc包中的drm()
结果),并在对数刻度中使用X轴。
I have a code like this: 我有这样的代码:
library(ggplot2)
args = list(A = 4.224536e+04, B = 1.335570e+00, C = 5.992606e-01, D = 2.341207e+04)
LP.4 = function(x, B, D, A, C) {D + (A-D)/(1+(x/C)^B)}
dose = as.numeric(rev(c(100, 5, 2, 1, 0.5 , 0.25 , 0.1 , 0.05, 0.01, 0.0001)))
predicted = lapply(dose, LP.4, args$B, args$D, args$A, args$C)
df = data.frame(cbind(as.numeric(dose), as.numeric(predicted)))
ggplot(df, aes(x=X1, y=X2)) +
scale_x_log10() +
geom_point()
Which generate points and axis exactly I need. 正是我需要生成点和轴。
I'm trying to plot function over this points using stat_function()
like this: 我正在尝试使用stat_function()
在这点上绘制函数,如下所示:
ggplot(data.frame(x=dose), aes(x)) +
scale_x_log10() +
stat_function(fun = LP.4,
args = list(A = 42245, B = 1.33, C = 0.599, D = 23412))
But I can't get the same result. 但我无法获得相同的结果。 What I'm doing wrong? 我做错了什么?
Your problem seems similar to the one resolved in this post . 你的问题似乎类似于一个在这个解决岗位 。 Using scale_x_log10, the base 10 log of x is taken and then passed to stat_function for evaluation. 使用scale_x_log10,x的基数10取对数,然后传递给stat_function进行评估。 Try something like the following: 尝试如下操作:
LP.4mod <- function(x, ...) LP.4(10^x,... )
ggplot(data.frame(x=dose), aes(x)) +
scale_x_log10() +
stat_function(fun = LP.4mod,
args = list(A = 42245, B = 1.33, C = 0.599, D = 23412))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.