作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有一种简单的方法可以在 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())
关于r - 游泳者生存图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33131184/
我每天都进行回归分析。就我而言,这通常意味着估计连续和分类预测变量对各种结果的影响。生存分析可能是我执行的最常见的分析。此类分析通常以非常方便的方式出现在期刊中。下面是一个例子: 我想知道是否有人遇到
我是一名优秀的程序员,十分优秀!