游泳者生存图 [英] swimmer survival plot

查看:120
本文介绍了游泳者生存图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以在 R 中生成游泳者图?与 KM 曲线中的数据相同,但每个个体的生存都用一条线表示.示例:

Is there an easy way to generate a swimmer plot in R? Same data as in a KM curve but with each individual survival represented as a line. Example:

我搜索了 stackoverflow、R-help 邮件列表,并咨询了 Google 博士,但没有得到明显的答案,尽管我的搜索技术可能不是最理想的.谢谢!

I've searched stackoverflow, the R-help mailing list, and consulted Dr. Google without an obvious answer, though my search technique may be suboptimal. Thank you!

**** 添加 ****为不恰当地提问而道歉 - 这是我第一次!玩玩,我已经能够做到以下几点:

**** ADDENDED **** Apologies for not appropriately asking a question - this is my first time! Playing around, I've been able to do the following:

          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)

这让我现在足够接近,但美学并不完美.如果有软件包或更好的解决方案,有人可以建议我很乐意看到它!

This gets me close enough for now, but aesthetics are not perfect. If there is a package or a better solution someone can suggest I'd love to see it!

推荐答案

您要求一种简单"的方法来生成游泳者图.这可能比您希望的要复杂一些,但它与您发布的内容非常接近.如果您需要制作大量游泳图,您可以将其调整为适合您的内容,然后将其转换为函数.

You asked for an "easy" way to generate a swimmer plot. This is probably a bit more involved than you were hoping for, but it's pretty close to what you posted. If you need to make a lot of swimmer plots, you can tweak this into something that works for you and then turn it into a function.

首先创建一些假数据:

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"))

现在开始剧情:

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())

这篇关于游泳者生存图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆