1
votes

I want to plot a function (result of using drm() from drc package) using ggplot2, with X-axis in log-scale.

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:

 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?

1
How about this link? It may help you.jazzurro

1 Answers

0
votes

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. 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))