Q:

Superimpose a function in ggplot2 with a time axis in R

I'm looking for an answer to a very specific problem.
I found the way to superimpose a function over a plot in ggplot2 at their page: http://docs.ggplot2.org/current/stat_function.html
But in my case I have a plot that uses time in the Y axis.
My data is:
dput(flN)
structure(list(eCenter = c(52, 85, 141, 227, 645), eLow = c(42, 
64, 112, 178, 546), eHigh = c(65, 112, 178, 290, 761), arrivalTime = structure(c(957173699, 
957173635, 957173496, 957173418, 957173338), class = c("POSIXct", 
"POSIXt"), tzone = ""), timeError = c(6.436288, 2.075383, 1.321365, 
1.270163, 3.422232), vCenter = c(125839365.727275, 154297213.515671, 
186197068.826928, 216301111.588418, 268912290.324273), vLow = c(114601800.781488, 
137454241.369541, 171493844.86893, 201095521.048661, 262431046.389897
), vHigh = c(138347098.798059, 171493844.86893, 201095521.048661, 
230862999.254391, 274537514.959924)), .Names = c("eCenter", "eLow", 
"eHigh", "arrivalTime", "timeError", "vCenter", "vLow", "vHigh"
), row.names = c("E1'", "E2'", "E3'", "E4'", "FP5'"), class = "data.frame")

Which looks like:
> flN
     eCenter eLow eHigh         arrivalTime timeError   vCenter      vLow     vHigh
E1'       52   42    65 2000-05-01 10:34:59  6.436288 125839366 114601801 138347099
E2'       85   64   112 2000-05-01 10:33:55  2.075383 154297214 137454241 171493845
E3'      141  112   178 2000-05-01 10:31:36  1.321365 186197069 171493845 201095521
E4'      227  178   290 2000-05-01 10:30:18  1.270163 216301112 201095521 230862999
FP5'     645  546   761 2000-05-01 10:28:58  3.422232 268912290 262431046 274537515

And the other quantities are:
m = 574.2538
c = 3E8
y0 = flN$arrivalTime - m*c/flN$vCenter
y01 = y0[1]

And the code I tried was:
#this works and plots a part of what I want:
p <- ggplot(flN, aes(x=c/vCenter, y=arrivalTime)) + geom_point(aes(y=arrivalTime)) + geom_errorbarh(aes(xmin=c/vHigh, xmax=c/vLow)) + xlim(0, 3)  + ylim(as.POSIXct('2000/05/01 10:20'), as.POSIXct('2000/05/01 10:40')) 

# the function I want to plot. It's a simple straight line function
test <- function(x) {y01 +m*x}

# the code to plot this function over my previous plotted data that doesn't work:
p + stat_function(fun = test, colour="#F8766D", linetype="dashed")
> Error: Discrete value supplied to continuous scale

Now, from the error, I believe the problem must be with the use of a time axis for Y but how do I solve this part of the problem?

A:

It seems that stat_function() gives error because it can't find variable y01 and m that are defined in global environment and calculates something else or give some value that is not continuous. One way to solve this problem is to modify your test() function by adding y01 and m as arguments and then in stat_function() add args=c(y01,m) - this will ensure that arguments are found and used correctly. Also I removed ylim() from your ggplot() call (it was outside data region at least for me).
test <- function(x,y01,m) {y01 +m*x}

p <- ggplot(flN, aes(x=c/vCenter, y=arrivalTime)) + 
     geom_point(aes(y=arrivalTime)) + 
     geom_errorbarh(aes(xmin=c/vHigh, xmax=c/vLow)) + xlim(0, 3)

p + stat_function(fun = test,args=c(y01,m), colour="#F8766D", linetype="dashed")

