0
votes

I wonder how I can plot only specific groups within a predefined strata. For instance in the examples below, how can I for instance plot only sex:0,rx:Obs against sex:1,rx:Obs as one specific survival-plot (not faceted), ignoring the other groups.

http://www.sthda.com/english/wiki/survminer-r-package-survival-data-analysis-and-visualization

enter image description here

2

2 Answers

0
votes

Generally R regression functions offer a 'subset' parameter that takes a logical expression as its argument. The expression will be (non-standard) evaluated in the frame of the data argument, so you can just give an unquoted column name followed by the equality operator and then the value (which will need to be quoted). So try this to limit the analysis to rx ==Obs:

fit2 <- survfit( Surv(time, status) ~ rx + adhere,
                 data = colon, subset = rx == "Obs" )
#------

> fit2
Call: survfit(formula = Surv(time, status) ~ rx + adhere, data = colon, 
    subset = rx == "Obs")

                   n events median 0.95LCL 0.95UCL
rx=Obs, adhere=0 536    287   1896    1447    2351
rx=Obs, adhere=1  94     58   1031     726    2077

(In the future you should post code and non-plotting output as text. I'm surprised that this has not been pointed out to you in comments before. I noticed that not many of your questions have been upvoted and this might be an explanation. People expect that they will be able to cut-and-paste code into their console session to test and offer output in their answers.)

0
votes

I come accross this months later as I am confronted with a similar question. Here are the different solutions I found. First, as mentioned by @IRTFM, the code to produce your plot:

library("survminer")
data=colon
fit2 <- survfit(Surv(time, status) ~ sex + rx,data = colon)
ggsurvplot_facet(fit2,colon,facet.by = "adhere",palette="jco",pval=TRUE)

Solution 1: First, we look at fit2

> fit2
Call: survfit(formula = Surv(time, status) ~ sex + rx, data = colon)

                    n events median 0.95LCL 0.95UCL
sex=0, rx=Obs     298    158   1981    1272      NA
sex=0, rx=Lev     266    137   1885    1275      NA
sex=0, rx=Lev+5FU 326    149     NA    2021      NA
sex=1, rx=Obs     332    187   1539    1195    2284
sex=1, rx=Lev     354    196   1548    1061    2593
sex=1, rx=Lev+5FU 282     93     NA      NA      NA

We can extract the strata of interest:

ggsurvplot(fit2[c(1,4)], colon, palette="jco",pval=TRUE)

enter image description here

Solution 2: for longer survfit objects, it can be tedious to grab the strata indexes, so we could use :

ggsurvplot(fit2[c(which(attr(fit2$strata,which="names")=="sex=0, rx=Obs    " | attr(fit2$strata,which="names")=="sex=1, rx=Obs    "))], 
           colon, palette="jco",pval=TRUE)

The results is the same graph as in solution 1 of course.

R is case (and space) sensitive, so the exact spelling of the strata can be extracted/checked with:

> attr(fit2$strata,"names")
[1] "sex=0, rx=Obs    " "sex=0, rx=Lev    " "sex=0, rx=Lev+5FU" "sex=1, rx=Obs    " "sex=1, rx=Lev    " "sex=1, rx=Lev+5FU"

Here, there are 3 spaces after "rx=Obs "

Solution 3: as suggested by @IRTFM, we can subset the data set (with rx and sex, not adhere). However, the p-value changes! In this case, I believe that not all data are used in the model, unlike the other solutions (? to be confirmed, if someone knows the details inside the calculation):

fit3 <- survfit( Surv(time, status) ~ rx + sex, data = colon, subset = rx == "Obs" )
> fit3
Call: survfit(formula = Surv(time, status) ~ rx + sex, data = colon, 
    subset = rx == "Obs")

                n events median 0.95LCL 0.95UCL
rx=Obs, sex=0 298    158   1981    1272      NA
rx=Obs, sex=1 332    187   1539    1195    2284

ggsurvplot(fit3, colon, palette="jco",pval=TRUE)

enter image description here

Solution 4: Instead of subsetting, one can also use

fit4 <- survfit( Surv(time, status) ~ rx + sex, data = colon[colon$rx=="Obs",])
fit4
Call: survfit(formula = Surv(time, status) ~ rx + sex, data = colon[colon$rx == 
    "Obs", ])

                n events median 0.95LCL 0.95UCL
rx=Obs, sex=0 298    158   1981    1272      NA
rx=Obs, sex=1 332    187   1539    1195    2284

ggsurvplot(fit4, colon, palette="jco",pval=TRUE)

The p-value is, however, similar as in solution 1 and 2 (p<0.0001) enter image description here

We can still facet the plot:

ggsurvplot_facet(fit4,data = colon[colon$rx=="Obs",],facet.by = "adhere",palette="jco",pval=TRUE)

enter image description here