Max Gordon
Max Gordon

Reputation: 5467

Getting plot boundaries in R

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

Upvotes: 4

Views: 3266

Answers (1)

IRTFM
IRTFM

Reputation: 263451

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.

Upvotes: 3

Related Questions