2015-10-14 16 views
6

C'è un modo semplice per generare una trama di nuotatore in R? Stessi dati di una curva KM ma con ogni sopravvivenza individuale rappresentata come una linea. Esempio:trama di sopravvivenza nuotatore

Ho cercato StackOverflow, la mailing list R-aiuto, e consultato il dottor Google senza una risposta ovvia, anche se la mia tecnica di ricerca potrebbe non essere ottimale. Grazie!

**** ADDENDED **** Ci scusiamo per non aver fatto correttamente una domanda - questa è la mia prima volta! Suonare in giro, sono stato in grado di effettuare le seguenti operazioni:

  OS DeathYN TreatmentGroup 
4 444 days  1    0 
5 553 days  1    0 
8 812 days  0    0 
1 844 days  0    0 
10 1071 days  0    0 
9 1147 days  0    0 
6 1349 days  0    0 
3 1375 days  0    0 
2 1384 days  0    1 
7 1687 days  0    0 

orderedData$GroupColor[orderedData$TreatmentGroup==0] <- "yellow" 
orderedData$GroupColor[orderedData$TreatmentGroup==1] <- "red" 
orderedData$YCoord <- barplot(as.numeric(orderedData$OS), horiz=TRUE, col=orderedData$GroupColor, xlim=c(0,max(orderedData$OS) + 50), xlab="Overall Survival") 
points(x=20+as.numeric(orderedData$OS), y=orderedData$YCoord,pch=62, col="green") 
legend(1000,2, c("Control", "Treatment", "still living"), col=c("yellow","red", "green"), lty=1, lwd=c(10,10,0),pch=62) 

Questo mi fa abbastanza vicino per ora, ma l'estetica non sono perfetti. Se c'è un pacchetto o una soluzione migliore qualcuno può suggerire che mi piacerebbe vederlo!

risposta

7

Hai chiesto un modo "facile" per generare un grafico del nuotatore. Questo è probabilmente un po 'più complicato di quanto speri, ma è abbastanza vicino a quello che hai postato. Se hai bisogno di creare molti diagrammi di nuotatori, puoi modificarlo in qualcosa che funziona per te e trasformarlo in una funzione.

Innanzitutto creare alcuni dati falsi:

library(ggplot2) 
library(reshape2) 
library(dplyr) 
library(grid) 

set.seed(33) 
dat = data.frame(Subject = 1:10, 
       Months = sample(4:20, 10, replace=TRUE), 
       Treated=sample(0:1, 10, replace=TRUE), 
       Stage = sample(1:4, 10, replace=TRUE), 
       Continued=sample(0:1, 10, replace=TRUE)) 

dat = dat %>% 
    group_by(Subject) %>% 
    mutate(Complete=sample(c(4:(max(Months)-1),NA), 1, 
         prob=c(rep(1, length(4:(max(Months)-1))),5), replace=TRUE), 
     Partial=sample(c(4:(max(Months)-1),NA), 1, 
         prob=c(rep(1, length(4:(max(Months)-1))),5), replace=TRUE), 
     Durable=sample(c(-0.5,NA), 1, replace=TRUE)) 

# Order Subjects by Months 
dat$Subject = factor(dat$Subject, levels=dat$Subject[order(dat$Months)]) 

# Melt part of data frame for adding points to bars 
dat.m = melt(dat %>% select(Subject, Months, Complete, Partial, Durable), 
      id.var=c("Subject","Months")) 

Ora, per la trama:

ggplot(dat, aes(Subject, Months)) + 
    geom_bar(stat="identity", aes(fill=factor(Stage)), width=0.7) + 
    geom_point(data=dat.m, 
      aes(Subject, value, colour=variable, shape=variable), size=4) + 
    geom_segment(data=dat %>% filter(Continued==1), 
      aes(x=Subject, xend=Subject, y=Months + 0.1, yend=Months + 1), 
      pch=15, size=0.8, arrow=arrow(type="closed", length=unit(0.1,"in"))) + 
    coord_flip() + 
    scale_fill_manual(values=hcl(seq(15,375,length.out=5)[1:4],100,70)) + 
    scale_colour_manual(values=c(hcl(seq(15,375,length.out=3)[1:2],100,40),"black")) + 
    scale_y_continuous(limits=c(-1,20), breaks=0:20) + 
    labs(fill="Disease Stage", colour="", shape="", 
     x="Subject Recevied Study Drug") + 
    theme_bw() + 
    theme(panel.grid.minor=element_blank(), 
     panel.grid.major=element_blank(), 
     axis.text.y=element_blank(), 
     axis.ticks.y=element_blank()) 

enter image description here

+0

Ciò è impressionante! Grazie! –

+1

dolce ......... –

Problemi correlati