[英]Which curve is which in Survival Function plot?
I am plotting survival functions with the survival package.我正在用 survival 包绘制生存函数。 Everything works fine, but how do I know which curve is which?一切正常,但我怎么知道哪条曲线是哪条曲线? And how can I add it to a legend?我怎样才能将它添加到图例中?
url <- "http://socserv.mcmaster.ca/jfox/Books/Companion/data/Rossi.txt"
Rossi <- read.table(url, header=TRUE)[,c(1:10)]
km <- survfit(Surv(week, arrest)~race, data=Rossi)
plot(km, lty=c(1 ,2))
how do I know which curve is which?我怎么知道哪条曲线是哪条曲线?
Using str()
you can see which elements are in km
.使用str()
您可以查看km
中有哪些元素。 km$strata
shows there are 48 and 10 elements. km$strata
显示有 48 个和 10 个元素。 This coincides with the declining pattern of the first 48 items and last 10 items in km$surv这与 km$surv 中前 48 项和后 10 项的下降模式一致
km$surv[1:48]
km$surv[49:58]
So in addition to the hint on order in print()
, with this particular dataset we can also be sure that the first 48 elements belong to race=black
因此,除了print()
中的顺序提示外,对于这个特定的数据集,我们还可以确定前 48 个元素属于race=black
And how can I add it to a legend?我怎样才能将它添加到图例中?
Unlike other model output km
is not easily transformed to a data.frame.与其他模型输出km
不同,它不容易转换为 data.frame。 However, we can extract the elements ourselves and create a data.frame and then plot it ourselves.但是,我们可以自己提取元素并创建一个 data.frame,然后自己绘制。
First we create a factor referring to the strata: 48 blacks and 10 others首先,我们创建一个与阶层有关的因素:48 名黑人和 10 名其他人
race <- as.factor(c(rep("black", 48), rep("other", 10)))
df <- data.frame(surv = km$surv, race = race, time = km$time)
Next we can plot it as usual (in my case, using ggplot2).接下来我们可以像往常一样绘制它(在我的例子中,使用 ggplot2)。
library(ggplot2)
ggplot(data = df, aes(x = time, y = surv)) +
geom_point(aes(colour = race)) +
geom_line(aes(colour = race)) +
theme_bw()
Unfortunately, the plot.survival
function doesn't seem to offer a nice option for labeling the curves.不幸的是, plot.survival
函数似乎没有提供一个很好的标记曲线的选项。 The documentation says that curves are plotted in the order they appear in print
, so you can figure out which is which when you vary line type or color.文档说曲线是按照它们在print
中出现的顺序绘制的,因此当您改变线型或颜色时,您可以弄清楚哪个是哪个。 But that's not great for sharing.但这不利于分享。
One alternative is to use the survplot
function from rms
, which does label the curves.一种替代方法是使用rms
中的survplot
函数,它会标记曲线。 Here's how that looks with your example and CI plotting turned off.这是您的示例和 CI 绘图关闭时的样子。 (Note that survplot
won't take a survfit
object, so you've got to redo the estimation with a function whose results it can read -- here, npsurv
.) (请注意, survplot
不会采用survfit
对象,因此您必须使用其结果可以读取的函数重新进行估计——此处为npsurv
。)
library(rms)
survplot(npsurv(Surv(week, arrest)~race, data=Rossi), conf = "none")
Check the documentation for ways to tweak other aspects of the chart, including replacing the labels in the plot with a legend.查看文档以了解调整图表其他方面的方法,包括用图例替换图中的标签。
Thanks to the answer of Richard, I found a way to plot the right names to the right curves, with the base plot way that is used in plot.survfit
:感谢理查德的回答,我找到了一种方法来绘制正确的名称到正确的曲线,使用plot.survfit
中使用的基本绘图方式:
legend_values <- names(km$strata)
plot(km)
legend(
"topright",
legend=legend_values,
col=1:2,
lty = c(1,1),
horiz=FALSE,
bty='n')
I prefer the ggplot way of plotting, but I like to retain the stepwise presentation in plot.survfit
.我更喜欢 ggplot 绘图方式,但我喜欢在plot.survfit
中保留逐步演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.