1

I have been trying to plot the survival function using the autoplot function in package survMisc. So far its good, the plot is fine, but the problem is I cannot use scale_color_grey to use the grey color scale.

The code is :

   autoplot(fit.bygroup, plotTable=TRUE, divideTime=1,legendLabs=c("group1", "group2"),  lty=c(1,2),geom_line(size=2))+ scale_color_grey()

I guess the problem is this autoplot is creating a blank object... Is there any way I can change them in to grey scale colors? Or what if I want a black and white background?

    p<- autoplot(fit.bygroup, plotTable=TRUE, divideTime=1,legendLabs=c("group1", "group2"),  lty=c(1,2),geom_line(size=2))

This p is actually NULL

Henrik
  • 65,555
  • 14
  • 143
  • 159
user2603847
  • 11
  • 1
  • 2
  • Yup thanks finally I actually used the plain plot function. But I think your answer is perfect. Yup ! Its true that I am new in Stackoverflow , but Thank you so much – user2603847 Mar 27 '14 at 06:36

2 Answers2

2

I think you need to change the code for the autoplot function used by survMisc. You find a very nice post on how to view source code here.

Type autoplot in console.

autoplot
# ...snip
# UseMethod("autoplot")
# ...snip

UseMethod("autoplot") means that autoplot is an S3 method. Then we can use methods to list available methods.

methods(autoplot)
# [1] autoplot.default* autoplot.survfit  autoplot.zoo

Type autoplot.survfit in console.

autoplot.survfit

Copy code to an editor.

Change colour scale:
Search for 'scale_col' and you will find three instances of:

scale_colour_brewer(type = "qual", palette = "Dark2",
                    guide = guide_legend(keywidth = 3, keyheight = 3))

Replace them with:

scale_colour_grey(guide = guide_legend(keywidth = 3, keyheight = 3))

Change background colour
In the second last code section:
Replace print(g1) with print(g1 + theme_classic()) (or any other theme you like)
Replace grid.arrange(arrangeGrob(g1 + theme(legend.position = "none"), with grid.arrange(arrangeGrob(g1 + theme_classic() + theme(legend.position = "none"),

Save the updated function with a new name, e.g. autoplot.survfit2.

Try it on first example in ?survMisc::autoplot

data(kidney, package = "KMsurv")
s1 <- survfit(Surv(time = time, event = delta) ~ type, data = kidney)
autoplot.survfit2(s1)

enter image description here

Community
  • 1
  • 1
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Yes, thank you so much, I tried to change the source code.....its working but I think there are some other issues that I need to solve, but thanks a lot. – user2603847 May 22 '14 at 04:36
0

As of version 0.4.2 this is a bit less 'hackey'. autoplot.survfit now returns a table and a plot in a list. These can be further modified as desired before combining them using autoplot.tableAndPlot as follows:

data(kidney, package = "KMsurv")
s1 <- survfit(Surv(time = time, event = delta) ~ type, data = kidney)
library(survMisc)
tap1 <- autoplot(s1)
for (i in seq_along(tap1)){
    tap1[[i]] <- tap1[[i]] + scale_colour_grey(guide = guide_legend(keywidth = 3, keyheight = 3)) + theme_classic()
    }
autoplot(tap1)

giving:

enter image description here

Or to modify the plot alone, something like:

p1 <- autoplot(s1)$plot
p1 +
    scale_colour_grey(guide = guide_legend(keywidth = 3, keyheight = 3)) +
    theme_classic()
dardisco
  • 5,086
  • 2
  • 39
  • 54