-1

I have the following call to ecdfplot in the latticeExtra package:

ecdfplot(~ saved_multtest + saved_unadjusted + saved_bonferroni + 
    saved_hochberg + saved_independent,
    data=saved, auto.key=list(corner=c(0.8,0.2)),
    main="Null case", xlab="p-value", 
    ylab="Empirical F(p-value)")

I would like to add a diagonal reference line, that is, a straight line from (0,0) to (1,1). I have searched the documentation for quite a while without finding out how to do this, perhaps because I am new at R. I would be grateful if someone could tell me how.

Neal Oden
  • 57
  • 4

2 Answers2

-1

Try:

lines(c(0,1), c(0, 1))

lines() lets you specify specific coordinates to join, whereas abline() only lets you specify a slope and intercept.

Darren
  • 333
  • 2
  • 10
  • Now the error message is "Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet." I think the diagonal line has to somehow be incorporated into the ecdfplot call, but I don't see how to do it. – Neal Oden Apr 28 '18 at 18:08
  • Ahh -- no, that's your issue. You don't add it to the original call to ecdfplot. You make the figure using ecdfplot, then once that is created the lines() or abline() call will add a line to the existing plot. That should take care of it, hopefully. – Darren Apr 28 '18 at 19:02
  • Just to follow-up on that, you see the "plot.new has not been called yet" in both error messages. That means that R is expecting to have an open plot window. You can create a blank plot with plot(NULL), even, and then create a plot with various calls to lines(), points() , etc. – Darren Apr 28 '18 at 19:04
  • I tried running your code -- I too got an error with the ecdf function. But have you tried this way? I didn't get any errors, and the lines() function added the line, as desired: x <- runif(1000); plot(ecdf(x)); lines(c(0,1), c(0,1)) So, this uses the base R ecdf function, which has an associated plot method. – Darren Apr 28 '18 at 20:00
  • The problem is that `latticeExtra::ecdfplot` uses `grid` graphics which are not compatible with standard R graphics functions. –  Apr 29 '18 at 11:02
-1

If you are using lattice (why???) then this will do what you want:

ecdfplot(rnorm(1000), panel = function (x, y) {
  panel.ecdfplot(x)
  panel.abline(0, 1)
})

Customize to taste.