0

I am trying to change the y-axis for my Kaplan Meier survival function. I am using autoplot to create a ggplot for a survfit object. It is in the survMisc library.

The output currently has the y-axis from 0 to 1, but I would like to change it to 0.5 to 1. There does not appear to be an argument within autoplot that allows me to change the axis.

If this is possible, I would also like to change the intervals of my tick marks. It currently has it at every 2.5 years (minor axis) but I would like it every year.

I have attached a sample survival code from the UCLA website, but my autoplot code is the same as in my script. Thanks.

hmohiv<-read.table("http://www.ats.ucla.edu/stat/r/examples/asa/hmohiv.csv", sep=",", header = TRUE) 
library(survival)
library(ggplot2)
library(Hmisc)
library(ggfortify)
attach(hmohiv)

hmohiv.surv <- survfit( Surv(time, censor)~ 1)
summary(hmohiv.surv)
autoplot(hmohiv.surv, type = "CI", palette="Set2", pVal=TRUE, 
                          title="Recurrence Free Survival", 

                          legTitle = "Adjuvant Treatment", 
                          xLab="Years to Recurrence", yLab="Probability",  
                          censSize = 2,
                          alpha = 0.9,
                          tabTitle = "Number at Risk",
                          tabTitleSize = 14,
                          tabLabSize = 2,
                          nRiskSize = 4,
                          timeTicks = "minor"
) + scale_y_continuous(limits=c(0.5,1))

It runs fine without the

+ scale_y_continuous(limits=c(0.5,1))

but when I run it with the scale, it gives me the following error:

Error in autoplot(hmohiv.surv, type = "CI", palette = "Set2", pVal = TRUE, : non-numeric argument to binary operator

jt.wingate
  • 49
  • 1
  • 8
  • 1
    You example isn't [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), but have you tried changing the limits as you would in *ggplot2*, with `ylim` or `scale_y_continuous` or `coord_cartesian`? The appropriate scale function is also where you could play around with the your breaks. – aosmith Nov 10 '15 at 23:34
  • Edited to show a sample code w/ freely available survival data. I have tried adding `scale_y_continuous` and it did not work. – jt.wingate Nov 11 '15 at 00:13
  • Did it not work due to error? Didn't do what you asked? Something else? It works for me (with the caveat that I'm using dev version of ggplot2 and had to load package *ggfortify* to get any of your code to run). – aosmith Nov 11 '15 at 00:21
  • I have edited to add the ggfortify and also the exact code I am writing and the error I am getting back. – jt.wingate Nov 11 '15 at 00:47

1 Answers1

2

You can either do (better to specify xlim also):

autoplot(hmohiv.surv, ylim = c(0.5, 1.0))

autoplot(hmohiv.surv) + ylim(c(0.5, 1.0))

enter image description here

Another option is use fortify to once convert survfit to data.frame, then filter and plot.

hmohiv.df <- fortify(hmohiv.surv) 
head(hmohiv.df)
#   time n.risk n.event n.censor      surv    std.err     upper     lower
# 1    1    100      15        2 0.8500000 0.04200840 0.9229465 0.7828189
# 2    2     83       5        5 0.7987952 0.05036890 0.8816770 0.7237046
# 3    3     73      10        2 0.6893712 0.06863972 0.7886410 0.6025969
# 4    4     61       4        1 0.6441665 0.07656258 0.7484595 0.5544060
# 5    5     56       7        0 0.5636457 0.09172158 0.6746519 0.4709043
# 6    6     49       2        1 0.5406398 0.09633941 0.6529986 0.4476141

ggfortify:::autoplot.survfit(hmohiv.df[hmohiv.df$surv > 0.5, ])

enter image description here

sinhrks
  • 1,306
  • 10
  • 5