


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
Typology: Study notes
1 / 26
This page cannot be seen from the preview
Don't miss anything!
Some minimal examples showing the output of plots from the examples.
library("survMisc")
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)
The ’autoplot’ function is a generic S3 method used by ’ggplot2’.
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)
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)
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
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)
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.
t4 <- ten(survfit(Surv(time, delta) ~ type, data=kidney)) (a4 <- autoplot(t4, type="CI", alpha=0.8, survLineSize=2)$plot)
suppressMessages(a4 + list( ggplot2::scale_color_manual(values=c("red", "blue")), ggplot2::scale_fill_manual(values=c("red", "blue"))))
suppressMessages(a4 + ggplot2::scale_y_continuous(limits=c(0, 1)))
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))
a