R Life Line Plots for Survival Analysis -


i plot life lines diagram data readers can understand how data shaped , right censoring data.

ideally [this][1]

i need horizontal line each participant, starting date of observation , ending on last day observed him. people having last day of observation should in different color (or have indicator).

the data this:

regdate             lastlogindate            censor   duration 2010-02-24 02:30:43 2010-05-27 07:58:17       0       92 2007-12-23 11:16:37 2008-03-07 10:36:29       1       75 2009-01-19 04:23:28 2009-01-24 06:33:38       1        5 2010-07-25 10:24:39 2010-08-11 07:13:25       0       17 2009-08-23 07:18:06 2009-08-24 06:25:35       1        1 2007-08-12 07:24:55 2010-06-01 06:53:57       0     1024 

ucla has how done in stata. told advisor can match whatever did in stata in r. in need of here guys :)

edit: managed right.

here sample of data dput.

structure(list(users_id = c(1747516, 913136, 921278, 1654913,  782364, 1371798, 1174461, 1493894, 1124186, 1249310),  regdate = c("2010-08-15 05:50:09", "2009-01-04 13:47:46", "2009-01-07 13:34:53", "2010-06-30 11:19:08", "2008-08-13 06:46:28", "2010-01-26 12:58:20", "2009-08-18 15:13:12", "2010-04-04 11:33:47", "2009-07-10 12:33:41", "2009-10-19 13:30:49" ),  lastlogindate = c("2010-09-01 05:51:34", "2010-09-17 05:25:00", "2009-05-15 07:55:30", "2010-07-02 07:34:02", "2008-10-25 14:29:50",  "2010-03-17 05:04:58", "2010-07-06 03:48:48", "2010-04-09 19:44:42", "2010-09-03 04:18:18", "2009-10-20 06:26:55"),  censor6 = c(0, 0, 1, 0, 1, 1, 0, 0, 0, 1)),  .names = c("users_id", "regdate", "lastlogindate", "censor6"),  row.names = c(1l, 2l, 4l, 5l, 7l, 9l, 10l, 11l, 12l, 14l),  class = "data.frame") 

what did melted data reshape2 package each observation there 2 rows. start , end dates. added censoring variable merge.

# create subset of data 25 observations sampdata1<-data[c("users_id", "regdate", "lastlogindate")] sampdata1<-sampdata1[sample(1:nrow(sampdata1),25),] # create 2 entries each observation 1 start date 1 end sampdata1<-melt(sampdata1, id.vars="users_id") sampdata1<-sampdata1[order(sampdata1$users_id, sampdata1$value),] # add grouping variable same thing user id looks better on plot sampdata1$id<-rep(seq(1,nrow(sampdata1)/2,1), each=2) # put censoring variable sampdata1<-merge(sampdata1, data[,c("users_id", "censor6")]) sampdata1$censor6<-as.factor(sampdata1$censor6) sampdata1$value<-as.posixct(sampdata1$value, origin="1970-01-01 00:00:00") 

now let create plot

# base plot gp<-ggplot(sampdata1)  # add horizontal lines (this big deal) gp+geom_line(aes(value, id, group=id, color=censor6, size=1))  # decluter x axis labels gp+scale_x_datetime(breaks=date_breaks('3 month')) # rotate x axis labels gp+ theme(axis.text.x = element_text(angle = 45, hjust = 1)) # change legend label , colors gp+scale_color_manual(values = c("red", "blue")) 

and here result.


Popular posts from this blog