R:如何标记绘图上的某些点

标签 r plot survival-analysis

library(survival)
> dput(kmsurvival)
structure(list(n = c(9L, 12L), time = c(28, 34, 38, 43, 44, 47, 
48, 54, 29, 39, 41, 47, 48, 49, 52, 53, 54, 58, 63), n.risk = c(9, 
8, 7, 6, 5, 4, 3, 1, 12, 11, 10, 9, 8, 7, 6, 4, 3, 2, 1), n.event = c(1, 
1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1), n.censor = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), surv = c(0.888888888888889, 
0.777777777777778, 0.666666666666667, 0.555555555555555, 0.444444444444444, 
0.333333333333333, 0.111111111111111, 0, 0.916666666666667, 0.833333333333333, 
0.75, 0.666666666666667, 0.583333333333333, 0.5, 0.333333333333333, 
0.25, 0.166666666666667, 0.0833333333333333, 0), type = "right", 
    strata = structure(c(8L, 11L), .Names = c("Type=BRCA1", "Type=BRCA2"
    )), std.err = c(0.117851130197758, 0.17817416127495, 0.235702260395516, 
    0.298142396999972, 0.372677996249965, 0.471404520791032, 
    0.942809041582063, Inf, 0.0870388279778489, 0.129099444873581, 
    0.166666666666667, 0.204124145231932, 0.243975018237133, 
    0.288675134594813, 0.408248290463863, 0.5, 0.645497224367903, 
    0.957427107756338, Inf), upper = c(1, 1, 1, 0.996567599682496, 
    0.922659733015886, 0.839728708174735, 0.70514430333281, NA, 
    1, 1, 1, 0.994625360259091, 0.940998012173809, 0.880421672024074, 
    0.741959705452895, 0.666102065388224, 0.590604814783314, 
    0.544229633514318, NA), lower = c(0.705557501515943, 0.548521190012713, 
    0.420028358578948, 0.309705006872564, 0.214088528120615, 
    0.132317866507894, 0.0175080177972009, NA, 0.772900973689615, 
    0.647036987013362, 0.540996355616556, 0.446846081150264, 
    0.361613705210385, 0.283954845665321, 0.149753565179511, 
    0.0938294643532942, 0.0470327655353929, 0.0127601365614755, 
    NA), conf.type = "log", conf.int = 0.95, call = survfit(formula = Surv(Time) ~ 
        Type, data = data2)), .Names = c("n", "time", "n.risk", 
"n.event", "n.censor", "surv", "type", "strata", "std.err", "upper", 
"lower", "conf.type", "conf.int", "call"), class = "survfit")

> plot(kmsurvival, fun = function(x) 1 - x, col = c("black", "red"), xlab = "Time",
+ ylab = "Cumulative Probability", main = "Cumulative Incidence", conf = FALSE)
> legend(0, 0.25, c("A", "B"), col = c("black", "red"), lty = c(1, 1))

enter image description here

我有一个累积发生率图,我想知道两条曲线(A 和 B)在时间 = 20、30、40、50 和 60 时的累积概率是多少,并标记这 10 个点。

最佳答案

我无法使用您的数据,因为 data2 丢失,但这里有一条类似的曲线。有关曲线的所有信息都包含在模型中。因此,您可以提取在特定时间标记点所需的内容。

## Make a similar fit
require(survival)
library(OIsurv)  # only needed for tongue data
data(tongue)
kmsurvival <- with(tongue, survfit(Surv(time) ~ type))

## Extract data from model
dat <- with(summary(kmsurvival),
            data.frame(surv=surv, type=as.integer(factor(strata)), time=time))

## Show points at these times
times <- with(dat, intersect(time[type==1], time[type==2]))
inds <- dat$time %in% times

## Make plot
plot(kmsurvival, fun = function(x) 1 - x, col = c("black", "red"), xlab = "Time",
     ylab = "Cumulative Probability", main = "Cumulative Incidence", conf = FALSE)
legend("topleft", 0.25, c("A", "B"), col = c("black", "red"), lty = c(1, 1))

## Add points/labels at times
with(dat, {
    points(time[inds], 1-surv[inds], col=type[inds], pch=16)
    inds1 <- inds & type==1  # points on first curve
    text(time[inds1], 1-surv[inds1], labels=round(1-surv[inds1], 2), adj=-0.5,
         col=type[inds1])
    inds2 <- inds & type==2  # points on second
    text(time[inds2], 1-surv[inds2], labels=round(1-surv[inds2], 2), adj=1.5,
         col=type[inds2])
})

enter image description here

关于R:如何标记绘图上的某些点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31957628/

相关文章:

r - XGBoost-具有变化的曝光/偏移的泊松分布

r - ggplot2-如何在离散刻度轴值中绘制连续值?

r - 如何沿染色体图形绘制位置

r - 将递归列表的列名称显示为树

Python IDLE 相当于 R 中的 CTRL-R

r - 如何固定文本的位置以跟随在 R 中绘制的对象

r - muhaz 包危险函数的置信区间

predict - 如何从 clogit 模型中获取拟合值

r - 用ggplot2在R中绘制生存曲线

r - 在给定另一列中相应的连续值相等的情况下,在一列中查找值的中位数