Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Plotting Survival Functions and Confidence Intervals using R: Examples and Customizations, Study notes of Advanced Computer Programming

Examples of how to generate survival plots and confidence intervals using R, specifically the 'survMisc' and 'ggplot2' packages. It covers various ways to modify the plots, such as changing line sizes, jittering points, customizing titles, and adjusting the legend. The document also demonstrates how to modify the y-axis limits and change the default colors.

What you will learn

  • What is the difference between pointwise and band confidence intervals in survival analysis?
  • How can I adjust the y-axis limits in a survival plot in R?
  • How can I generate a survival plot using R?
  • What packages are required to generate survival plots in R?
  • How do I modify the title of a survival plot in R?

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

lumidee
lumidee 🇺🇸

4.4

(47)

364 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Examples of output from plotting functions
C Dardis
November 22, 2016
Some minimal examples showing the output of plots from the examples.
1 plotSurv
library("survMisc")
## Loading required package: survival
## Loading required package: splines
df0 <- data.frame(t1=c(0,2,4,6,NA,NA,12,14),
t2=c(NA,NA,4,6,8,10,16,18))
s1 <- Surv(df0$t1, df0$t2, type="interval2")
plot(s1, l=2)
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Plotting Survival Functions and Confidence Intervals using R: Examples and Customizations and more Study notes Advanced Computer Programming in PDF only on Docsity!

Examples of output from plotting functions

C Dardis

November 22, 2016

Some minimal examples showing the output of plots from the examples.

1 plotSurv

library("survMisc")

Loading required package: survival

Loading required package: splines

df0 <- data.frame(t1=c(0, 2, 4, 6, NA, NA, 12, 14), t2=c(NA, NA, 4, 6, 8, 10, 16, 18)) s1 <- Surv(df0$t1, df0$t2, type="interval2") plot(s1, l=2)

2 autoplot.Ten

The ’autoplot’ function is a generic S3 method used by ’ggplot2’.

2.1 Simple examples

Warning: Ignoring unknown aesthetics: x, y

A more customized example follows. Note that we return only the element marked ’plot’ from the result (which is a list with two elements).

autoplot(t1, timeTicks="months", type="CI", jitter="all", legLabs=c("surgical", "percutaneous"), title="Time to infection following catheter placement \n by type of catheter, for dialysis patients",

titleSize=10, censSize=2)$plot

Here we assign the result in order to modify the y axis.

str(a1 <- autoplot(t1), max.level=1)

List of 2

$ table:List of 10

..- attr(*, "class")= chr [1:2] "gg" "ggplot"

$ plot :List of 9

Here we also re-label the legend.

autoplot(b1, legOrd=c(3, 2, 1), legLabs=letters[1:3])

Now, let’s put the legend inside the plot itself.

a2 <- autoplot(b1)

ensure this is what we want

a2$plot + ggplot2::theme(legend.position=c(0.75, 0.75)) a2$plot <- a2$plot + ggplot2::theme(legend.position=c(0.75, 0.75)) a

2.3 One group only

A number of options for plotting a line with just one group.

t2 <- ten(survfit(Surv(time=time, event=delta) ~ 1, data=kidney)) autoplot(t2, legLabs="")$plot autoplot(t2, legend=FALSE)

2.5 More customization

If the output of ’autoplot.ten’ is assigned, it can be modified in place. The list elements are ggplot2 objects which can be altered as usual.

manually changing the output

t4 <- ten(survfit(Surv(time, delta) ~ type, data=kidney)) (a4 <- autoplot(t4, type="CI", alpha=0.8, survLineSize=2)$plot)

change default colors

suppressMessages(a4 + list( ggplot2::scale_color_manual(values=c("red", "blue")), ggplot2::scale_fill_manual(values=c("red", "blue"))))

change limits of y-axis

suppressMessages(a4 + ggplot2::scale_y_continuous(limits=c(0, 1)))

3 autoplot.StratTen

An example of the plots from a stratified model:

data("pbc", package="survival") t1 <- ten(Surv(time, status==2) ~ trt + strata(edema), data=pbc, abbNames=FALSE) suppressWarnings(str(a1 <- autoplot(t1), max.level=1))

List of 3

$ edema=0.5=FALSE, edema=1=FALSE:List of 2

..- attr(*, "class")= chr [1:2] "tableAndPlot" "list"

$ edema=0.5=FALSE, edema=1=TRUE :List of 2

..- attr(*, "class")= chr [1:2] "tableAndPlot" "list"

$ edema=0.5=TRUE, edema=1=FALSE :List of 2

..- attr(*, "class")= chr [1:2] "tableAndPlot" "list"

- attr(*, "class")= chr [1:2] "stratTableAndPlot" "list"

a

Warning: Removed 2 rows containing missing values (geom text).

Warning: Removed 2 rows containing missing values (geom text).