Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. library(survival)
  2. library(ggplot2)
  3. data(lung)
  4. s <- survfit(Surv(time, status) ~ 1, data = lung)
  5. dat <- data.frame(time = c(0, s$time),
  6. surv = c(1, s$surv),
  7. nr = c(s$n, s$n.risk))
  8. pl1 <- ggplot(dat, aes(time, surv)) + geom_step()
  9.  
  10. pl2 <- ggplot(dat, aes(time, nr)) + geom_area()
  11.  
  12. library(reshape2)
  13. dat.long<-melt(dat,id.vars="time")
  14. head(dat.long)
  15. time variable value
  16. 1 0 surv 1.0000000
  17. 2 5 surv 0.9956140
  18. 3 11 surv 0.9824561
  19. 4 12 surv 0.9780702
  20. 5 13 surv 0.9692982
  21. 6 15 surv 0.9649123
  22.  
  23. ggplot()+geom_step(data=subset(dat.long,variable=="surv"),aes(time,value))+
  24. geom_area(data=subset(dat.long,variable=="nr"),aes(time,value))+
  25. facet_grid(variable~.,scales="free_y")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement