4

My question: How do I find the boundaries for the plot area?

I'm using the plotHR function for plotting splines from cox regressions. I've done some adaptations found here and the density plot annoys me so I want to be able to change it to a simple regular polygon without the need for using a second plot. The problem is that the second plot needs repositioning as soon as the medium size changes.

Now I know I can use the plot_boundaries.y and plot_boundaries.x together with the limits and adding the xaxs/yaxs information but this seems like not the most intuitive way of doing this. Is there an easy way of getting my plots bottom left x,y and top right x,y?

Here's an example of how to use the plotHR:

library(survival)

hmohiv<-read.table("http://www.ats.ucla.edu/stat/R/examples/asa/hmohiv.csv", sep=",", header = TRUE)

surv <- with(hmohiv, Surv(time, censor))
fit <- coxph(surv~ pspline(age), data=hmohiv)
par(xaxs="i", yaxs="i")
plotHR(fit, bty="l", ylim=c(.4, 5), y.ticks=c(.5, 1, 1.5, 2, 3, 4), xlim=c(25, 55))
Max Gordon
  • 5,367
  • 2
  • 44
  • 70
  • 2
    see `?par`, especially `par("usr")` – Ben Bolker Jan 08 '12 at 17:46
  • possible duplicate of [Get plot() bounding box values](http://stackoverflow.com/questions/7322301/get-plot-bounding-box-values) – joran Jan 08 '12 at 22:38
  • @joran For the benefit of others who happen by in the future, can you clarify why a question about boundaries for `plotHR()` is a duplicate of a question on `plot()` boundaries? I have no insight into `plotHR`, but [my experience with `ggplot2`](http://stackoverflow.com/questions/7705345/how-can-i-extract-plot-axes-ranges-for-a-ggplot2-object) makes me reluctant to assume that plot boundaries are trivial to obtain. :) – Iterator Jan 09 '12 at 00:39
  • 1
    @Iterator My reasoning is simply that the way to find the plot boundaries in _base graphics_ is to use `par("usr")` as Ben notes; so (nearly) any question about finding the plotting area boundaries in base graphics is essentially the same, and will have the same answer, regardless of the specific function at play. lattice and ggplot both use grid graphics, which works a bit differently. – joran Jan 09 '12 at 00:53
  • Thanks, I just didn't consider par()... The usr doesn't strike me as an intuitive option for plot boundaries but I guess there must be some devine wisdom behind it - sorry for posting a duplicate, guess I didn't use the right key words when searching – Max Gordon Jan 09 '12 at 05:53
  • 1
    @MaxGordon Much of what's in `par` is only intuitive after you've had to look it up 5-6 times and have learned that it's there. And don't worry about the dupe; it's good to have a few duplicates around for precisely the reason you mentioned: not everyone searches for things the same way. – joran Jan 09 '12 at 17:29

1 Answers1

3

If you are using pkg:survival, (or any plotting function that depends on base graphics as does the plotHR function that yopu have now made available in the greg package) then this should work:

plot(survfit(fit))
par("usr")
# [1]  0.0 62.4  0.0  1.0

The "bottom-left" x-y point is par("usr")[c(1, 3)] and the upper right is par("usr")[c(2, 4)]. Thanks for publishing that function in the Greg package package.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks. The plotHR is linked in my question, it's a small but very neat way of plotting splines. At least it's the best one I've found so far – Max Gordon Jan 09 '12 at 05:55