3
$\begingroup$

I have been looking for ways to plot survival times as in the following graph Survival Times

And having this data:

agestart agestop dead dead1 dead2 dead3 dead4

40.00    60.80   0    0     0     0     0
44.43    57.65   1    0     0     1     0
40.00    60.38   0    0     0     0     0
41.11    66.29   0    0     0     0     0 
47.06    65.97   1    0     1     0     0

But I'd like to plot them without colors, i.e. just indicating censoring with a cross, death1 with a circle, death2 with a square, death3 with a star, and so on. Is there any easy solution for this type of graphics in R? I have only found graphs that assume the same starting point in the study, for all participants

$\endgroup$
2
$\begingroup$

Using a slightly larger dataset than what you provided, but with the same properties, here is one possible solution using ggplot2.

First the data:

 ID agestart  agestop dead dead1 dead2 dead3 dead4
  1 40.31898 61.07525    0     0     0     0     0
  2 44.73464 70.82110    0     0     0     0     0
  3 38.69327 61.53224    1     0     0     1     0
  4 45.93688 61.47643    1     0     0     0     1
  5 43.10441 65.73701    0     0     0     0     0
  6 41.00229 59.95093    1     1     0     0     0
  7 42.93796 63.48322    1     0     1     0     0
  8 44.75329 59.98213    1     0     0     0     1
  9 49.28906 65.60382    0     0     0     0     0
 10 40.89990 61.11743    1     0     0     1     0
 11 40.42592 56.77811    1     1     0     0     0
 12 38.43149 59.91117    1     0     1     0     0
 13 44.80300 61.63504    0     0     0     0     0
 14 40.81832 63.97311    1     0     0     0     1
 15 43.36405 66.56718    0     0     0     0     0
 16 39.25923 60.56785    0     0     0     0     0
 17 43.07955 63.19032    1     0     0     1     0
 18 48.35370 66.20075    0     0     0     0     0
 19 41.00028 61.21209    1     0     0     0     1
 20 43.78243 63.84572    1     0     0     1     0

Next some code to create a string variable with your different death options (where dat is your original data frame):

dat$death<-rep(NA, length(dat[,1]))
dat$death[dat$dead==0]<-'Alive'
dat$death[dat$dead1==1]<-'Death1'
dat$death[dat$dead2==1]<-'Death2'
dat$death[dat$dead3==1]<-'Death3'
dat$death[dat$dead4==1]<-'Death4'

Then to work in ggplot2 - selecting out the relevant variables and reshaping the data from wide to long:

DF<-dat[,c(1,2,3,9)]#selects relevant columns in this particular data frame

library(reshape2)

DF.long<-reshape(DF, 
             v.names = 'age', 
             varying = c('agestart', 'agestop'), 
             timevar = 'Timepoint', 
             times = c('Start', 'Stop'), 
             direction = 'long')

And finally the code for ggplot:

library(ggplot2)
g1<-ggplot(aes(y=ID, x=age, group =ID), data=DF.long)
g2<-g1+geom_line()
g3<-g2+geom_point(aes(shape=death))
g4<-g3+ggtitle('Awesome Title Goes Here!')
g5<-g4+xlab('Age')
g6<-g5+ylab('Study ID')
g6

And since I like controlling the resolution of my output:

jpeg('Sample File.jpeg', res = 300, units='in', width = 6, height = 6)
g6
dev.off()

Which yields: enter image description here

The shape of the points and their size can be adjusted according to your preference. You can also alter the line type and make other adjustments to the plot.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.