了解 coxph 模型的 R survfit.plot 函数中的颜色
Understand colors in R survfit.plot function for a coxph model
我正在尝试创建一组具有分类解释变量的单变量 coxph 模型,并且对于每个模型,我想按变量级别比较生存函数。例如我正在创建:
library(survival)
set.seed(4321)
data <- data.frame(cbind(
sample(0:1, 40, replace=TRUE), # event
sample(0:24, 40, replace=TRUE), # time
sample(1:2, 40, replace=TRUE), # Diam
sample(3:4, 40, replace=TRUE) # N
))
colnames(data) <- c("event","time","Diam","N")
data$Diam <- as.factor(data$Diam)
data$N <- as.factor(data$N)
model1 <- coxph(Surv(time, event) ~ Diam, data = data)
model2 <- coxph(Surv(time, event) ~ N, data = data)
其中 Diam 和 N 有两个级别。
我准备了这段代码来绘制生存函数:
plot(survfit(model1, newdata = data), fun = "s",
conf.int = TRUE,
col = 2:4,
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by Diam")
legend("bottomleft",
legend = levels(data$Diam),
lty = 1, col = c(2,4))
plot(survfit(model2, newdata = data), fun = "s",
conf.int = TRUE,
col = 2:4,
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by N")
legend("bottomleft",
legend = levels(data$N),
lty = 1, col = c(2,4))
这是输出:
我不明白:
- 为什么第二张图表的颜色不同?
- 如何匹配图表和图例中的颜色。
如有任何帮助,我们将不胜感激!
先感谢您,
弗朗西斯卡
我觉得很奇怪,你画图的时候放了三种颜色,图例里只放了两种。我设法用这个
正确地绘制了它
plot(survfit(model1, newdata = data), fun = "s",
conf.int = TRUE,
col = c(1,2,2),
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by Diam")
legend("bottomleft",
legend = levels(data$Diam),
lty = 1, col = levels(data$Diam))
plot(survfit(model2, newdata = data), fun = "s",
conf.int = TRUE,
col = levels(data$N),
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by N")
legend("bottomleft",
legend = levels(data$N),
lty = 1, col = levels(data$N))
但请注意,我在第一个图中保留了三种颜色,而在第二个图中只保留了两种。我认为问题出在模型或采样上。
此外,使用不同的种子(例如 1),颜色不会有任何问题。
set.seed(1)
data <- data.frame(cbind(
sample(0:1, 40, replace=TRUE), # event
sample(0:24, 40, replace=TRUE), # time
sample(1:2, 40, replace=TRUE), # Diam
sample(3:4, 40, replace=TRUE) # N
))
colnames(data) <- c("event","time","Diam","N")
data$Diam <- as.factor(data$Diam)
data$N <- as.factor(data$N)
model1 <- coxph(Surv(time, event) ~ Diam, data = data)
model2 <- coxph(Surv(time, event) ~ N, data = data)
plot(survfit(model1, newdata = data), fun = "s",
conf.int = TRUE,
col = levels(data$Diam),
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by Diam")
legend("bottomleft",
legend = levels(data$Diam),
lty = 1, col = levels(data$Diam))
plot(survfit(model2, newdata = data), fun = "s",
conf.int = TRUE,
col = levels(data$N),
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by N")
legend("bottomleft",
legend = levels(data$N),
lty = 1, col = levels(data$N))
干杯,
我正在尝试创建一组具有分类解释变量的单变量 coxph 模型,并且对于每个模型,我想按变量级别比较生存函数。例如我正在创建:
library(survival)
set.seed(4321)
data <- data.frame(cbind(
sample(0:1, 40, replace=TRUE), # event
sample(0:24, 40, replace=TRUE), # time
sample(1:2, 40, replace=TRUE), # Diam
sample(3:4, 40, replace=TRUE) # N
))
colnames(data) <- c("event","time","Diam","N")
data$Diam <- as.factor(data$Diam)
data$N <- as.factor(data$N)
model1 <- coxph(Surv(time, event) ~ Diam, data = data)
model2 <- coxph(Surv(time, event) ~ N, data = data)
其中 Diam 和 N 有两个级别。 我准备了这段代码来绘制生存函数:
plot(survfit(model1, newdata = data), fun = "s",
conf.int = TRUE,
col = 2:4,
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by Diam")
legend("bottomleft",
legend = levels(data$Diam),
lty = 1, col = c(2,4))
plot(survfit(model2, newdata = data), fun = "s",
conf.int = TRUE,
col = 2:4,
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by N")
legend("bottomleft",
legend = levels(data$N),
lty = 1, col = c(2,4))
这是输出:
我不明白:
- 为什么第二张图表的颜色不同?
- 如何匹配图表和图例中的颜色。
如有任何帮助,我们将不胜感激! 先感谢您, 弗朗西斯卡
我觉得很奇怪,你画图的时候放了三种颜色,图例里只放了两种。我设法用这个
正确地绘制了它plot(survfit(model1, newdata = data), fun = "s",
conf.int = TRUE,
col = c(1,2,2),
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by Diam")
legend("bottomleft",
legend = levels(data$Diam),
lty = 1, col = levels(data$Diam))
plot(survfit(model2, newdata = data), fun = "s",
conf.int = TRUE,
col = levels(data$N),
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by N")
legend("bottomleft",
legend = levels(data$N),
lty = 1, col = levels(data$N))
但请注意,我在第一个图中保留了三种颜色,而在第二个图中只保留了两种。我认为问题出在模型或采样上。
此外,使用不同的种子(例如 1),颜色不会有任何问题。
set.seed(1)
data <- data.frame(cbind(
sample(0:1, 40, replace=TRUE), # event
sample(0:24, 40, replace=TRUE), # time
sample(1:2, 40, replace=TRUE), # Diam
sample(3:4, 40, replace=TRUE) # N
))
colnames(data) <- c("event","time","Diam","N")
data$Diam <- as.factor(data$Diam)
data$N <- as.factor(data$N)
model1 <- coxph(Surv(time, event) ~ Diam, data = data)
model2 <- coxph(Surv(time, event) ~ N, data = data)
plot(survfit(model1, newdata = data), fun = "s",
conf.int = TRUE,
col = levels(data$Diam),
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by Diam")
legend("bottomleft",
legend = levels(data$Diam),
lty = 1, col = levels(data$Diam))
plot(survfit(model2, newdata = data), fun = "s",
conf.int = TRUE,
col = levels(data$N),
xlab = "Months",
ylab = "Proportion without event",
main = "Survival curve by N")
legend("bottomleft",
legend = levels(data$N),
lty = 1, col = levels(data$N))
干杯,