gpt4 book ai didi

r - 游泳者生存图

转载 作者:行者123 更新时间:2023-12-03 03:44:36 24 4
gpt4 key购买 nike

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

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

**** 已添加 ****很抱歉没有适本地提出问题 - 这是我第一次!通过尝试,我能够执行以下操作:

          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)

目前这已经足够接近了,但美学并不完美。如果有一个包或更好的解决方案,有人可以建议我很乐意看到它!

最佳答案

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

首先创建一些虚假数据:

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

enter image description here

关于r - 游泳者生存图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33131184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com